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

Default value for priority doesn't get applied #190

Open
IboKhaled opened this issue Jul 19, 2023 · 0 comments
Open

Default value for priority doesn't get applied #190

IboKhaled opened this issue Jul 19, 2023 · 0 comments

Comments

@IboKhaled
Copy link

According to the documentation, when priority is left undefined, it should default to 0. However this doesn't seem to be the case.
Instead, the promises seem to be executed in a random order (not the order they were inserted into the queue in!).
When manually setting the priority to 0, the queue behaves as expected.

Example code:

import PQueue from "p-queue";

function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function addToQueue(name: string, priority?: number) {
  console.log(`adding ${name} to queue`);
  return queue.add(
    async () => {
      await delay(1000);
      console.log(`finished ${name}`);
      return Promise.resolve();
    },
    { priority }
  );
}

const queue = new PQueue({ concurrency: 1 });
const first = addToQueue("first");
const second = addToQueue("second");
const priority = addToQueue("priority", 1);
const third = addToQueue("third");
Example output Expected output
adding first to queue adding first to queue
adding second to queue adding second to queue
adding priority to queue adding priority to queue
adding third to queue adding third to queue
finished first finished first
finished third finished priority
finished priority finished second
finished second finished third
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant