,

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 *

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.