wesbos
wesbos

Reputation: 26317

AsyncJS with a for loop

I'm looking to do something fairly simple with Nodejs and Async.

I have a number of pages, say 4 for our example. And I want to make a request 4 times and then trigger a callback when they have all returned.

  async.eachSeries new Array(pages)
    ,(i,next)->
      offset+=100;
      next();
    ,(err)->
      console.log("All done!");

Is there an async method can I use a for loop in? Or do I need to loop over and create the functions first, and then pass to async?

Update: is the above the best way to do this?

Upvotes: 0

Views: 359

Answers (1)

Andreas Hultgren
Andreas Hultgren

Reputation: 14953

async.times(4, function(n, next){
    somethingAsync(n, next);
},
function (err) {
    // Here when all four calls are done
});

See async.times.

Upvotes: 2

Related Questions