Radhika Desai
Radhika Desai

Reputation: 51

async.eachLimit executes only for the limit specified and not the whole array

I am using npm async library (https://caolan.github.io/async/docs.html) to limit the number of parallel requests. Below is my code :

    async.eachLimit(listOfItemIds, 10, function(itemId)
    {
      console.log("in with item Id: ",itemId);
    }, function(err) {
         if(err) 
         {
           console.log("err : ",err);
           throw err;
         }
    });

But it does not execute for all the listOfItemIds, it only executes for the first 10 and exits.

Below is the output :

in with item id:  252511893899
in with item id:  142558907839
in with item id:  273235013353
in with item id:  112966379563
in with item id:  192525382704
in with item id:  253336093614
in with item id:  112313616389
in with item id:  162256230991
in with item id:  282981461384
in with item id:  263607905569

Upvotes: 4

Views: 5877

Answers (1)

zenwraight
zenwraight

Reputation: 2000

You need to pass along a callback() method as well.

Here have a look at the code below, this will work.

async.eachLimit(listOfItemIds, 2, function(itemId, callback)
{
  console.log("in with item Id: ",itemId);
  callback();
}, function(err) {
      if(err) 
      {
        console.log("err : ",err);
        throw err;
      }
});

This will print all the elements in the array, I have limit the number of parallel executions to be 2 in the above case.

Hope this helps!

Upvotes: 7

Related Questions