Skip to content
Advertisement

async.queue drain function won’t fire

I had an async.queue implementation on my node js app but the queue.drain function recently stopped firing at all.

I suspected the issue was related to await statements I have inside task functions but I also am able to reproduce the issue using the sample on async docs

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');
});

This will output the following on my console but not the drain statement. So is there something I am missing?

hello bar

hello foo

finished processing foo

Advertisement

Answer

Interestingly, converting the drain function into an arrow function resolved the issue.

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

q.drain(() => {
  console.log('all items have been processed');
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement