Skip to content

Instantly share code, notes, and snippets.

@seanstrom
Last active August 29, 2015 14:13
Show Gist options
  • Save seanstrom/d4235271e7974fbc8027 to your computer and use it in GitHub Desktop.
Save seanstrom/d4235271e7974fbc8027 to your computer and use it in GitHub Desktop.
Async JS/Node - Sequential Promises Example
function findLikesForPostsByUsername(username, callback) {
var promise = new Promise(function(resolver, rejector) {
findUserByUsername(username)
.then(findPostsByUser)
.then(findLikesByPosts)
.then(function(likes) {
var totalLikes = likes.reduce(function(x, y) { return x + y })
resolver(totalLikes)
})
.catch(function(err) {
rejector(err)
})
})
return promise
}
var username = 'seanghagstrom'
findLikesForPostsByUsername(username)
.then(function(totalLikes) {
console.log(username + ' total number of likes: ' + totalLikes)
})
.catch(function(err) {
console.error(err)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment