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

Getting queued results back in order #167

Open
pzich opened this issue Aug 29, 2022 · 0 comments
Open

Getting queued results back in order #167

pzich opened this issue Aug 29, 2022 · 0 comments

Comments

@pzich
Copy link

pzich commented Aug 29, 2022

Hello there!

I just started using PQueue in a few projects and it was super easy to integrate, great work!

One issue I'm running into is the ordering of the results. In my case I'm loading and processing chunks of data, and the amount of time that takes can vary, so sometimes later chunks are done processing sooner. I've made a contrived example to illustrate the issue.

import PQueue from 'https://esm.run/p-queue'

const pre = document.querySelector('pre');

const queue = new PQueue({ concurrency: 4 });

const loader = (v) => new Promise(resolve => {
  console.log('Loading', v);
  setTimeout(() => {
    console.log('Loaded', v);
    resolve(v);
  }, (Math.random() * 2 + 2) * 1000);
});

const printLn = (v) => {
  console.log('Printing', v);
  pre.innerText += `${v}\n`;
}

for (let i = 0; i < 20; i++) {
  queue.add(() => loader(i).then(printLn));
}

Screen Shot 2022-08-29 at 1 09 14 PM

What I'd like to be able to do is hold onto and wait on resolving these later queued results until the ones before have all resolved, then resolve these in order up to the latest resolved item in the queue.


I think this functionality is somewhat decoupled / orthogonal to PQueue itself, but I think it would be helpful to include as an option for the queue, as well as a standalone promise-fun package.

Apologies if this is already implemented here or as part of promise-fun. I looked through the docs here and all of the promise-fun packages that sounded relevant/similar, but didn't see anything quite like this.

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