Vivek Goel
Vivek Goel

Reputation: 24140

Async each series and parallel execution

I have any array: Example array = [1 to 1 million ] currently, I am doing :

async.eachSeries(array,function(item,callback){
some async (
callback
)
});

I want to do: 5 parallel execution, is it possible using async?

Upvotes: 1

Views: 884

Answers (1)

jfriend00
jfriend00

Reputation: 707158

The async library has a lot of different options that will run a fixed number of requests in parallel. But, note that as soon as you run multiple operations in parallel, there is no guarantee on the order in which the ones in parallel will complete as that depends upon the server and what order it returns them.

Here are some of your options:

eachLimit

Iterate each item in the array with up to 5 operations in flight at the same time. No results are collected. For eachLimit() it is assumed that you either don't need results or you are collecting your own results as a side effect of running these operations.

async.eachLimit(array, 5, function(item, callback) {
   // process each item here, call callback when done
}, function(err) {
   // done with all of them here
});

mapLimit

Kind of like array.map() in that it iterates the array and collects the results in order in a results array. Also allows you to specify how many operation you want in flight at the same time. Note that individual operations may not run exactly in order (they will be requested in order, but may finish out of order), but the results array available at the end will have all results in proper order.

async.mapLimit(array, 5, function(item, callback) {
   // process each item here, call callback when done with the result value
   // for each request
}, function(err, results) {
   // done with all of them here and array of results (in order)
});

Upvotes: 2

Related Questions