I have to post a bunch of requests to the same endpoint with different payloads. The payloads are all in a loop and I felt async.parallelLimit
used alongwith async.reflect
would solve the issue. But I ran into the closure and scope issue with this. I know how to address this with a setTimeout
using the third parameter. Below is my snippet of code,
var tasks = [];
for ( i in items ){
if ( items[i] ){
tasks.push(async.reflect(function(callback) {
axios.post(storeEndpoint, items[i])
.then(callback)
.catch(function(err){
console.log('error', err);
});
}));
}
}
async.parallelLimit(tasks, 10, (err, results) => {
console.log("res: ", results.length);
console.log('done');
});
Any pointers are appreciated.