DEV Community

Latz
Latz

Posted on

NodeJS: async.queue does not display error messages by default

Currently, I’m creating a custom web crawler and the easiest method to implement concurrency is to use the npm package “async“.

While coding, I came across the problem, that the program wasn’t working correctly, but the terminal didn’t display any errors.

So I tested the queued function outside of async, and it threw an error as expected. What was happening here?

It turned out that async.queue is catching errors by default and that you have to handle them explicitly. This can be done by adding an error callback to the queue:

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

q.error(function(err, task) {
    console.error('task experienced an error');
});
Enter fullscreen mode Exit fullscreen mode

Currently, this not very helpful but of course you can display the error easily:

q.error(function(err, task) {
    console.error(`Async error: ${err}`);
});
Enter fullscreen mode Exit fullscreen mode

There we have it:

Async error: ReferenceError: xxx is not defined

Top comments (0)