Skip to content

Instantly share code, notes, and snippets.

@seanstrom
Last active August 29, 2015 14:13
Show Gist options
  • Save seanstrom/a3f4f7cc226d884e8388 to your computer and use it in GitHub Desktop.
Save seanstrom/a3f4f7cc226d884e8388 to your computer and use it in GitHub Desktop.
Async JS/Node - Parallel Callbacks Example
function getTweetsPostsRepos(username, callback) {
var count = 0
, results = []
function somethingFinished(err, result) {
count += 1
results.push(result)
if(count === 3) {
callback(null, results)
}
}
getTweets(username, function(err, results) {
if(err) return callback(err)
somethingFinished(null, results)
})
getPosts(username, function(err, results) {
if(err) return callback(err)
somethingFinished(null, results)
})
getRepos(username, function(err, results) {
if(err) return callback(err)
somethingFinished(null, results)
})
}
getTweetsPostsRepos(username, function(err, tweetsPostsRepos) {
if(err) {
console.error(err)
} else {
console.log(username + ' has this stuff ' + tweetsPostsRepos)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment