Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
danday74 opened this issue Dec 12, 2022 · 2 comments
Labels

Comments

@danday74
Copy link

danday74 commented Dec 12, 2022

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
Copy link

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 aearly added the queue label Dec 2, 2023
@aearly
Copy link
Collaborator

aearly commented Dec 2, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants