Skip to content

Instantly share code, notes, and snippets.

@kainazzzo
Created July 7, 2014 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kainazzzo/9f35ade891016d176190 to your computer and use it in GitHub Desktop.
Save kainazzzo/9f35ade891016d176190 to your computer and use it in GitHub Desktop.
How I would write the promise chain from https://gist.github.com/anonymous/97108e545cb800025b33
function getUsernamePromise(input) {
if (isNumber(input)) {
return getUserNameById(input);
}
if (isString(input)) {
return getUsernameByEmail(input);
}
if (isObject(input)) {
return Account.find().where(input).exec()
}
}
function authenticate(input) {
return getUsernamePromise(input)
.then(function (username) {
return getUser(username);
})
// chained because we will not need the user name in the next event
.then(function (user) {
return getPassword()
// nested because we need both user and password next
.then(function (password) {
if (user.passwordHash !== hash(password)) {
throw new Error("Can't authenticate");
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment