admin管理员组文章数量:1425156
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.
本文标签: javascriptIs it possible to limit async tasks in nodeStack Overflow
版权声明:本文标题:javascript - Is it possible to limit async tasks in node? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745396676a2656846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论