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?
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?
Share Improve this question edited Dec 5, 2019 at 22:04 Max 1,0601 gold badge12 silver badges20 bronze badges asked Jan 18, 2016 at 22:13 MickeyThreeShedsMickeyThreeSheds 732 silver badges9 bronze badges 3- 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 Commented Jan 18, 2016 at 22:17
-
You didn't even take a look at the docs, did you? Just use
async.eachLimit
. – Bergi Commented Jan 18, 2016 at 22:17 - Possible duplicate of Limiting asynchronous calls in Node.js – Andreas Commented Jan 18, 2016 at 22:17
3 Answers
Reset to default 5I 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');
}
}
);
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;
});
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745396676a4625910.html
评论列表(0条)