-
-
Save anonymous/97108e545cb800025b33 to your computer and use it in GitHub Desktop.
here's how a promise chain that has multiple input (including recursive input) looks
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 authenticate(input) { | |
if(isNumber(input)) { | |
return getUsernameById(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"); | |
} | |
}); | |
}); | |
} else if(isString(input)) { | |
return getUsernameByEmail(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"); | |
} | |
}); | |
}); | |
} else if(isObject(input)) { | |
return Account.find().where(input).exec() | |
.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"); | |
} | |
}); | |
}); | |
} else if(isArray(input)) { | |
// lolwut | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment