Skip to content

Instantly share code, notes, and snippets.

@AmarPrabhu
Created August 26, 2013 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmarPrabhu/6339909 to your computer and use it in GitHub Desktop.
Save AmarPrabhu/6339909 to your computer and use it in GitHub Desktop.
iterate over a collection, perform an asynchronous task for each item, but only let x tasks run at the same time, and when they're all done do something else. From: http://www.sebastianseilund.com/nodejs-async-in-practice
app.delete('/messages/:messageIds', function(req, res, next) {
var messageIds = req.params.messageIds.split(',');
async.forEachLimit(messageIds, 5, function(messageId, callback) {
db.delete('messages', messageId, callback);
}, function(err) {
if (err) return next(err);
res.json({
success: true,
message: messageIds.length+' message(s) was deleted.'
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment