async icon indicating copy to clipboard operation
async copied to clipboard

with async.queue order of execution is different when using promises instead of callbacks but should be identical

Open danday74 opened this issue 1 year ago • 2 comments

What version of async are you using?

3.2.4

Which environment did the issue occur in (Node/browser/Babel/Typescript version)

Node

What did you do? Please include a minimal reproducible case illustrating issue.

Using async.queue I get different execution results when I use promises instead of callbacks.

https://stackblitz.com/edit/node-qeye7d?file=index.js

You can execute the code in this example by typing node index in the stackblitz terminal.

What did you expect to happen?

Results should be the same.

What was the actual result?

Results were different in terms of order of execution.


Here's my code:

const async = require('async');

const myFunc = async () => {
  // CALLBACKS
  console.log();
  console.log('CALLBACKS');

  const worker1 = (job, callback) => {
    console.log('job started', job.name);
    setTimeout(() => callback(null, job), 1000);
  };

  const q1 = async.queue(worker1, 1);

  q1.push({ name: 'job1' }, (err, job) => {
    console.log('job done', job);
  });
  q1.push({ name: 'job2' }, (err, job) => {
    console.log('job done', job);
  });

  await q1.drain();

  // PROMISES
  console.log();
  console.log('PROMISES');

  const worker2 = async (job) => {
    return new Promise((resolve) => {
      console.log('job started', job.name);
      setTimeout(() => resolve(job), 1000);
    });
  };

  const q2 = async.queue(worker2, 1);

  q2.push({ name: 'job1' }).then((job) => {
    console.log('job done', job);
  });
  q2.push({ name: 'job2' }).then((job) => {
    console.log('job done', job);
  });

  await q2.drain();
};

myFunc();

Actual output is:

CALLBACKS job started job1 job done { name: 'job1' } job started job2 job done { name: 'job2' }

PROMISES job started job1 job started job2 job done { name: 'job1' } job done { name: 'job2' }

Expected output is:

CALLBACKS job started job1 job done { name: 'job1' } job started job2 job done { name: 'job2' }

PROMISES job started job1 job done { name: 'job1' } job started job2 job done { name: 'job2' }

danday74 avatar Dec 12 '22 11:12 danday74

It seems that the queue actually waits for the job to end to start the new one but there is a difference on when the callback is called in terms of async logic: https://stackblitz.com/edit/node-1fslka?file=index.js

raphael-verdier avatar Jan 23 '23 11:01 raphael-verdier

I'm not sure this is an actual issue? There will always be subtle differences in timing with using callbacks vs functions. If you need to the task callback to not block the event loop, you can always defer inside it.

aearly avatar Dec 02 '23 22:12 aearly