To execute a function within a loop in node.js, it's fine to use a for
loop for short loops. But the loop is long, using for
loop will increase the time of processing which might cause the node process to hang. In such scenarios, you can use: asycn.times
function recursiveAction(n, callback)
{
//do whatever want to do repeatedly
callback(err, result);
}
async.times(5, function(n, next) {
recursiveAction(n, function(err, result) {
next(err, result);
});
}, function(err, results) {
// we should now have 5 result
});
This is called in parallel. When we want to call it one at a time, use: async.timesSeries