Last active
March 31, 2016 16:55
-
-
Save anshdivu/b452e53e144aa06c592bd1118515d84c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getUserByEmailAddress(email) { | |
return User.first({emailAddress: email}).chain(function(user) { | |
if (!user) { | |
return ErrorPromises.NotFound(`No user found for emailAddress = ${email}`) | |
} | |
return user; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Regex to find
.chain
withif
condition as the first line =>chain\s*\(\s*function\s*\(\s*([^)]+?)\s*\)\s*{\n*\s*if