Async: Awaitable q.push doesn't resolve when queue worker throws an error

Created on 6 Jun 2019  ·  6Comments  ·  Source: caolan/async

await q.push(task) is not safe for use for the moment because the promise returned by q.push never resolve nor reject if the queue worker throws an error. I understand that await q.push(task) cannot throw an error because of the backward compatibility of this method and its optional callback parameter (the push and forget feature). A q.asyncPush(task) method that rejects its promise shouldn't be feasible ?

You can show the problem with this code (in node or browser without the require):

const async = require('async');
let pushResult;

(async () => {
    const q = async.queue(async (task) => {
        throw new Error('Bad thing');
    });

    try {
        pushResult = q.push({some: 'data'});
        await pushResult;
        console.log('Error not catched');
    } catch (e) {
        console.log('Error catched');
    }
})();

setTimeout(() => {
    console.log('exit');
    console.log(pushResult);
}, 1000);

_Originally posted by @darksabrefr in https://github.com/caolan/async/pull/1641#issuecomment-498191809_

bug feedback-wanted

Most helpful comment

I like the asyncPush idea.

All 6 comments

I'm considering making q.push() resolve with the error object. Would that be sufficient? I really don't wan't to give unhandledRejections to the vast majority of queue users.

Resolving with an error may be an option but you will need to test the q.push return, it's not really a promise conventional pattern (no try/catch/finally block or .then()/.catch() methods can be used with). Direct rejections are not an option, I agree with it, it's a major modification. For me, there is 2 other possibilities:

  • a distinct q.asyncPush method that returns a promise that can reject. Maybe the easiest and the clearest way because the promise and callback APIs can be fully separated.
  • a boolean as second parameter of q.push to activate promises rejects.

The really important thing is that promises will not be forever pending, so you can select the best option you think ;-)

I like the asyncPush idea.

Thank you very much !

@aearly was this ever implemented?

Was this page helpful?
0 / 5 - 0 ratings