Skip to content

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

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