-
-
Save aheuermann/0f603776db40ab087fa24741aa5d941c 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 returing `null` | |
// advantage - don't have to write a reject handler | |
// disadvantage - possibly forget 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 | |
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}; | |
}); | |
}).chain(function(err) { | |
if(err.objectNoFound()) { | |
return {available: true}; | |
} else { | |
return comb.rejected(); | |
} | |
}).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