Skip to content

Instantly share code, notes, and snippets.

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