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

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

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

There we have it:

Async error: ReferenceError: xxx is not defined


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post’s permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post’s URL again. (Find out more about Webmentions.)