Skip to content

Instantly share code, notes, and snippets.

@aaronpeterson
Created July 22, 2013 04:33
Show Gist options
  • Save aaronpeterson/6051292 to your computer and use it in GitHub Desktop.
Save aaronpeterson/6051292 to your computer and use it in GitHub Desktop.
An untested example of async.each to decorate a list of users.
// populate users with User.find, etc, then inside that callback:
var results = [];
async.each(users, function(user, cb) {
Foo.find({"user._id":user._id}, function(err, result) {
// note: return isn't returning anything valuable, it's just a shortcut to leave the function to avoid ugly } else {
if (err) return cb(err);
if (result === true) {
// whatever this is...
user.foo = 'bar';
}
results.push(user);
cb(null);
});
}, function(err) {
// if you have err here, one of your callbacks was called with a non-null first arg.
if (err) return cb(err);
console.log('your users have been modified and the results are here:', results);
res.send(200, results); // or some callback, etc
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment