Skip to content

Instantly share code, notes, and snippets.

@ybogdanov
Created August 20, 2011 13:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ybogdanov/1159101 to your computer and use it in GitHub Desktop.
Save ybogdanov/1159101 to your computer and use it in GitHub Desktop.
nodejs callbackDriven vs node-async vs node-sync
function getUserSummary(userId, callback)
{
async.parallel({
user : function(callback) {
db.users.findUserById(userId, callback)
},
tweets : function(callback) {
db.users.getTweets(userId, callback)
},
getFollowers : function(callback) {
db.users.getFollowers(userId, callback)
},
getFollowing : function(callback) {
db.users.getFollowing(userId, callback)
}
}, callback)
}
function getUserSummary(userId, callback)
{
db.users.findUserById(userId, function(err, user){
if (err) return callback(err);
db.users.getTweets(userId, function(err, tweets){
if (err) return callback(err);
db.users.getFollowers(userId, function(err, followers){
if (err) return callback(err);
db.users.getFollowing(userId, function(err, following){
if (err) return callback(err);
callback(null, {
user : user,
tweets : tweets,
followers : followers,
following : following
})
})
})
})
})
}
var getUserSummary = function(userId)
{
return {
user : db.users.findUserById(userId),
tweets : db.users.getTweets(userId),
followers : db.users.getFollowers(userId),
following : db.users.getFollowing(userId)
}
}.async()
var getUserSummary = function(userId)
{
var dbUsers = db.users;
var user = dbUsers.findUserById.future(dbUsers, userId),
tweets = dbUsers.getTweets.future(dbUsers, userId),
followers = dbUsers.getFollowers.future(dbUsers, userId),
following = dbUsers.getFollowing.future(dbUsers, userId);
return {
user : user.result,
tweets : tweets.result,
followers : followers.result,
following : following.result
}
}.async()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment