Don't worry Ben
You will see why in about 2 minutes
function createComment (comment) {
const canComment = userCanCreateComment(); // 1 Permission Check
if (!canComment) throw new Error("User cant comment");
const validComment = commentIsValid(comment); // 2 Validate Comment
if (!validComment) throw new Error("Invalid Comment");
const success = postNewComment(comment); // 3 Post to server
if (!success) throw new Error("Could not save post");
return comment; // 4 return comment
}function createComment (comment, callback) {
try {
userCanCreateComment((err) => { // 1 Permission Check
// if err
commentIsValid(comment, (err) => { // 2 Validate Comment
// if err
postNewComment(comment, (err) => { // 3 Post to server
// if err
if (callback) callback(comment); // 4 return comment
});
});
});
} catch (err) {
// do something with error
}
}function createComment (comment, callback) {
try {
userCanCreateComment((err) => { // 1 Permission Check
// if err
commentIsValid(comment, (err) => { // 2 Validate Comment
// if err
postNewComment(comment, (err) => { // 3 Post to server
// if err
if (callback) callback(comment); // 4 return comment
});
});
});
} catch (err) {
// do something with error
}
}Doom
function createComment (comment) {
return new Promise ((resolve, reject) => {
userCanCreateComment() // 1 Permission Check
.then(commentIsValid(comment), handleBadPermission) // 2 Validate Comment
.then(postNewComment(comment), handleBadComment) // 3 Post to Server
.then(() => resolve(comment), handleBadPost) // 4 Return Comment
.catch(err => reject(err, data))
})
}
// but we obscure the complexity away
function commentIsValid(comment) {
return function() {
return new Promise ((resolve, reject) => {
// the thing that actually checks if
// the comment is valid
return validatorThing(comment)
})
};
}A coroutine is a function that can suspend its execution (yield/wait) until the given Yield Instruction is completed
function createComment (comment) {
return coroutine(function * () {
const canComment = yield userCanCreateComment(); // 1 Permission Check
if (!canComment) throw new Error("User cant comment");
const validComment = yield commentIsValid(comment); // 2 Validate Comment
if (!validComment) throw new Error("Invalid Comment");
const success = yield postNewComment(comment); // 3 Post to Server
if (!success) throw new Error("Could not save post");
return comment; // 4 Return Comment
})();
}function createComment (comment) {
const canComment = userCanCreateComment(); // 1 Permission Check
if (!canComment) throw new Error("User cant comment");
const validComment = commentIsValid(data); // 2 Validate Comment
if (!validComment) throw new Error("Invalid Comment");
const success = postNewComment(comment); // 3 Post to server
if (!success) throw new Error("Could not save post");
return comment; // 4 return comment
}async function createComment (comment) {
const canComment = await userCanCreateComment(); // 1 Permission Check
if (!canComment) throw new Error("User cant comment");
const validComment = await commentIsValid(comment); // 2 Validate Comment
if (!validComment) throw new Error("Invalid Comment");
const success = await postNewComment(comment); // 3 Post to Server
if (!success) throw new Error("Could not save post");
return comment; // 4 Return Comment
} function createComment (comment) {
const canComment = userCanCreateComment(); // 1 Permission Check
if (!canComment) throw new Error("User cant comment");
const validComment = commentIsValid(comment); // 2 Validate Comment
if (!validComment) throw new Error("Invalid Comment");
const success = postNewComment(comment); // 3 Post to Server
if (!success) throw new Error("Could not save post");
return comment; // 4 Return Comment
}function createComment (comment) {
const canComment = userCanCreateComment(); // 1 Permission Check
if (!canComment) throw new Error("User cant comment");
const validComment = commentIsValid(comment); // 2 Validate Comment
if (!validComment) throw new Error("Invalid Comment");
const success = postNewComment(comment); // 3 Post to Server
if (!success) throw new Error("Could not save post");
return comment; // 4 Return Comment
}