Created
February 1, 2012 02:24
-
-
Save fredrick/1714667 to your computer and use it in GitHub Desktop.
Async.js and Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
// Waterfall through callbacks using Mongoose to query for a user | |
async.waterfall([ | |
function(callback) { | |
/** Function 1. Find User. | |
* Pass `user` to Function 2. | |
*/ | |
User.findOne({ username: 'fred' }, function(error, user) { | |
if (error) throw error; | |
callback(error, user); | |
}); | |
}, | |
/** Function 2. Receive User. | |
* Pass `user` to final function | |
*/ | |
function(user, callback) { | |
callback(user); | |
} | |
], function(user) { | |
// Final function | |
console.log(user); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks