First, you must add a callback to your iterator function:
var requestApi = function(data, next){ request(data.url, function (error, response, body) { console.log(body); next(error); }); };
next(); or next(null); tells Async that all processing is complete. next(error); indicates an error (if error not null ).
After processing all the requests, Async calls the callback function with err == null :
async.forEachLimit(data, 5, requestApi, function(err){ // err contains the first error or null if (err) throw err; console.log('All requests processed!'); });
After receiving the first error or after successfully completing all asynchronous calls, its callback immediately succeeds.
source share