nodejs Async:concurency worker在队列中再次推送相同的任务

我想知道在完成使用nodejs的asynchronous模块之后,无限期地再次在队列中推送新任务的最佳方式是什么?

var q = async.queue(function (task, callback) { console.log('hello ' + task.name); doSomeFunction(task.name, function(cb){ callback(); }); }, 2); q.drain = function() { console.log('all items have been processed'); } // add some items to the queue for (var i in list) { q.push({name: i}, function (err) { console.log('finished task'); //***HERE I would like to push indefinitely this task in the queue again }); } 

你必须做一个recursion函数。

 for (var i in list) { //Put inside an anonymous function to keep the current value of i (function(item) { var a=function(item){ q.push({name: item}, function (err) { console.log('finished task'); //call the function again a(item) }); } a(item) })(i); } 

这个鳕鱼将无限期地逐个添加队列中的所有任务(当一个任务完成时,而不是同一个任务被添加到队列中)。

顺便说一句…你没有在工作人员function中调用callback

 var q = async.queue(function (task, callback) { console.log('hello ' + task.name); //You have to call the callback //You have 2 options: doSomeFunction(task.name,callback); //option 1 -> doSomeFunction - asynchronous function //doSomeFunction(task.name);callback(); //option 2 doSomeFunction - synchronous function }, 2);