async.queue 排水功能不会触发

Sac*_*rab 0 javascript queue asynchronous node.js async.js

我的 Node js 应用程序上有一个 async.queue 实现,但queue.drain 函数最近完全停止触发。

我怀疑该问题与我内部任务函数中的等待语句有关,但我也可以使用异步文档上的示例重现该问题

const async = require('async')

var q = async.queue(function(task, callback) {
  console.log('hello ' + task.name);
  callback();
}, 1);

q.drain(function() {
  console.log('all items have been processed');
});

q.push({name: 'bar'});
q.push({name: 'foo'}, function(err) {
  console.log('finished processing foo');
});
Run Code Online (Sandbox Code Playgroud)

这将在我的控制台上输出以下内容,但不会输出排出语句。那么我缺少什么吗?

你好酒吧

你好富

处理完成 foo

Sac*_*rab 6

有趣的是,将排出函数转换为箭头函数解决了这个问题。

q.drain = function () {
  console.log('all items have been processed');
}

q.drain(() => {
  console.log('all items have been processed');
}
Run Code Online (Sandbox Code Playgroud)

  • 您没有转换该函数。第一个分配一个函数并覆盖drain,第二个使用您的函数的参数调用drain (6认同)