Skip to content

Instantly share code, notes, and snippets.

/q.js Secret

Created July 7, 2014 01:16
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 anonymous/97108e545cb800025b33 to your computer and use it in GitHub Desktop.
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
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