Reputation: 143
I have a queue that consumes commands based on https://caolan.github.io/async/v3/docs.html#queue with an async function, because the processing requires async/await.
this.commandQueue = async.queue(async (task, callback) =>
{
await this.sleep(10); // Long running async stuff
callback(null, data);
}, 1);
The result of the task shall be sent back via 'data'.
this.commandQueue.push(
{
...command data
}, function (err, data)
{
// called when task finished - callback called
... // data is undefined
});
Issue: 'data' is undefined.
When I remove async /await from the top function section, it works, but I can't call my long-running task :-(
I have no idea how to solve this issue. Any hints?
Upvotes: 3
Views: 850
Reputation: 31
See here https://caolan.github.io/async/v3/global.html. Async function won't get the callback argument from ES2017 on. Have a look what ES version you're using.
Workaround is be make the worker function not "async" but promised based. If anyone knows how to fix it without switching to promises, comment would be appreciated.
// async bases
async function worker(opts, callback) { // callback will be undefined}
const q = async.queue(worker)
//promised based
function worker(opts, callback) { // callback will work as expected}
const q = async.queue(worker)
Wherever we accept a Node-style async function, we also directly accept an ES2017 async function. In this case, the async function will not be passed a final callback argument, and any thrown error will be used as the err argument of the implicit callback, and the return value will be used as the result value. (i.e. a rejected of the returned Promise becomes the err callback argument, and a resolved value becomes the result.)
Upvotes: 3