caolan / async

Async utilities for node and the browser
http://caolan.github.io/async/
MIT License
28.15k stars 2.41k forks source link

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

Closed danday74 closed 1 week ago

danday74 commented 1 year ago

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' }

raphael-verdier commented 1 year ago

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

aearly commented 9 months ago

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.