MickeyThreeSheds
MickeyThreeSheds

Reputation: 73

Is it possible to limit async tasks in node?

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?

Upvotes: 3

Views: 4094

Answers (3)

Ionut
Ionut

Reputation: 1749

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');
        }
    }
);

Upvotes: 5

Borys Serebrov
Borys Serebrov

Reputation: 16172

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.

Upvotes: 0

Rosenumber14
Rosenumber14

Reputation: 165

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;
});

Upvotes: 0

Related Questions