3

I have the following section of async code:

async.forEach(list, function(file, loopCallback) {
    console.log("file");
    loopCallback();
}, {
    console.log("all done!");
});

It prints the name of all my files in my list, great. Now, I want to limit the amount of files I am processing in parallel. What if I only want to handle one file at a time?

I have heard of async.ParallelLimit, and async.Queue, and would like to know how to adapt this code to fit one of them, specifically parallelLimit.

Any ideas?

Max
  • 1,054
  • 1
  • 12
  • 20
  • so... you know that async has options that do what you want, but you want us to convert your code to one of those other options.. – Kevin B Jan 18 '16 at 22:17
  • You didn't even take a look at [the docs](https://github.com/caolan/async#documentation), did you? Just use `async.eachLimit`. – Bergi Jan 18 '16 at 22:17
  • Possible duplicate of [Limiting asynchronous calls in Node.js](http://stackoverflow.com/questions/9539886/limiting-asynchronous-calls-in-node-js) – Andreas Jan 18 '16 at 22:17

3 Answers3

5

I think what you need is eachLimit, not parallelLimit. Here is an example:

async.each(
    list,
    1, // limit
    function(file, callback) {
        console.log('Processing file ' + file);
        callback();
    },
    function(err){
        if( err ) {
          console.log('Failed to process');
        } else {
          console.log('All files have been processed successfully');
        }
    }
);
Ionut
  • 1,729
  • 4
  • 23
  • 50
0

You could try using (every) instead of foreach

var count = 0;
var totalcount = 5;
async.every(function() {
// Do something.
count++;
if (count == 5)
   return false;
   else return true;
});
Rosenumber14
  • 165
  • 12
0

I think that async.mapLimit is what you need:

async.mapLimit([1,2,3,4,5], 3, function(value, loopCallback) {
    // will process by 3 items at time
    console.log(value);
    loopCallback(value);
}

It is mentioned in the map description.

Borys Serebrov
  • 15,636
  • 2
  • 38
  • 54