Created
July 7, 2014 12:14
-
-
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
This file contains hidden or 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 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