Skip to content

Instantly share code, notes, and snippets.

@anshdivu
Last active March 31, 2016 16:55
Show Gist options
  • Save anshdivu/b452e53e144aa06c592bd1118515d84c to your computer and use it in GitHub Desktop.
Save anshdivu/b452e53e144aa06c592bd1118515d84c to your computer and use it in GitHub Desktop.
// promise handles user not found by returning `null`
// advantage - don't have to write a reject handler
// disadvantage - requires developer to know the implementation detail of the facade function
// that it returns `null` or not. Higher possibility of forgeting to write `null`
// check and cause null pointer exception
function checkEmail(emailAddress) {
return facade.user.getUserByEmailAddress(emailAddress).chain(function (user) {
if (!user) {
return {available: true};
}
return facade.user.isUserAnInvitedTaker(user).chain(function (isInvited) {
return isInvited ? {invited: true} : {available: false}
});
}).chain(res.handler.successHandler(), res.handler.errorHandler(errors.UNKNOWN_ERROR));
}
// promise handles user not found by rejecting the promise and returning an error
// advantage - success handler handles the common case and never causes a null pointer exception
// disadvantage - recovering from `not found` is explicit and has to occur in reject handler
// requires better understanding of errors in the case we need to recover from the errors
function checkEmail(emailAddress) {
return facade.user.getUserByEmailAddress(emailAddress).chain(function (user) {
return facade.user.isUserAnInvitedTaker(user).chain(function (isInvited) {
return isInvited ? {invited: true} : {available: false};
});
}, function(err) {
if (err.objectNoFound()) {
return {available: true};
}
return comb.rejected(err);
}).chain(res.handler.successHandler(), res.handler.errorHandler(errors.UNKNOWN_ERROR));
}
function getUserByEmailAddress(email) {
return User.first({emailAddress: email}).chain(function(user) {
if (!user) {
return ErrorPromises.NotFound(`No user found for emailAddress = ${email}`)
}
return user;
});
}
// https://github.com/C2FO/c2fo/blob/develop/api/lib/registration/routes.js#L50-L62
function checkEmail(emailAddress) {
return facade.user.getUserByEmailAddress(emailAddress).chain(function (user) {
if (user) {
return facade.user.isUserAnInvitedTaker(user).chain(function (isInvited) {
if (isInvited) {
res.handler.success({invited: true});
}else {
res.handler.success({available: false});
}
});
} else {
res.handler.success({available: true});
}
}, res.handler.errorHandler(errors.UNKNOWN_ERROR));
}
@anshdivu
Copy link
Author

Regex to find .chain with if condition as the first line => chain\s*\(\s*function\s*\(\s*([^)]+?)\s*\)\s*{\n*\s*if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment