async.times to handle a for loop in a better way

suggest change

To run a function several times in Node.js, a for loop is fine when the loop is short. If the loop is long and each step runs synchronously, it can block the event loop. In that situation you can use async.times, which invokes the iteratee n times and yields between turns:

function recursiveAction(n, callback)
{
    // do whatever you want to do repeatedly
    callback(null, n);
}
async.times(5, function(n, next) {
    recursiveAction(n, function(err, result) {
        next(err, result);
    });
}, function(err, results) {
    // we should now have 5 results
});

This is called in parallel. When we want to call it one at a time, use: async.timesSeries

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents