Created
July 22, 2013 04:33
-
-
Save aaronpeterson/6051292 to your computer and use it in GitHub Desktop.
An untested example of async.each to decorate a list of users.
This file contains 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
// 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