我试图了解来自link的异步队列。以下是本教程中的代码,但做了一些修改。我尝试递归地调用async.queue而不是繁琐的任务,但是它仅在5个任务之后停止。该程序不应该无限运行吗?而且从来没有调用taskQueue.drain。我想念什么?
我还提到了issue on github,它说是将其推送到队列中,然后调用我执行的回调。
var async = require('async');
var _ = require('lodash');
//Generate an array of 10 random tasks;
var tasksList = _.times(10, _.uniqueId.bind(null, 'task_'));
var tasksQueue = async.queue(function (task, callback) {
console.log('Performing task: ' + task.name);
console.log('Waiting to be processed: ', tasksQueue.length());
console.log('----------------------------------');
//Simulate intensive processing
tasksList.push(_.times(10, _.uniqueId.bind(null, 'task_')) );
// If you want to pass an error object here, it will be caught in the task handler
// callback('something went wrong');
_.each(tasksList, function (task) {
tasksQueue.push({ name: task }, function (err) {
//Done
if (err) {
console.log(err);
}
callback();
});
});
}, 5);
// When all is processed, drain is called
tasksQueue.drain = function() {
console.log('all items have been processed.');
};
_.each(tasksList, function(task) {
tasksQueue.push({name: task}, function(err) {
//Done
if (err) {
console.log(err);
}
});
});