Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
async.queue with async handler function - callback argument undefined

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!