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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay