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

Helper method to emulate add(() => {}) #82

Open
snario opened this issue Sep 11, 2019 · 5 comments
Open

Helper method to emulate add(() => {}) #82

snario opened this issue Sep 11, 2019 · 5 comments

Comments

@snario
Copy link

snario commented Sep 11, 2019

As far as I can tell, there is no way to get a promise that resolves for all items that are currently in the queue to finish (even if new ones are added later). A hacky workaround is const p = q.add(() => {}) which does get you this, but at the expense of adding an item to the queue.

@kevva
Copy link

kevva commented Sep 30, 2019

Doesn't .onIdle do pretty much what you want?

@snario
Copy link
Author

snario commented Oct 1, 2019

When I was testing this I noticed that if I do something like this:

const queue = new Queue({ concurrency: 1 });
const p1 = queue.add(() => { ... });
const p2 = queue.add(() => { ... });
const waitPromise = queue.onIdle();
const p3 = queue.add(() => { ... });

If p2 has not yet begun executing when p3 is assigned, then waitPromise will only resolve after p3 begins executing. But what I need is for waitPromise to resolve when p2 begins executing (i.e., when the queue was idle at the point that onIdle was called).

@kevva
Copy link

kevva commented Oct 1, 2019

You should use await queue.onIdle() since it returns a promise. Same with queue.add() really.

@snario
Copy link
Author

snario commented Oct 3, 2019

My use case requires me to add to many queues simultaneously, not that code snippet precisely. Specifically what I need is for a promise to be added to n queues at the same time, and wait for everything to be finished in every queue that it was added to before running that promise.

@snario
Copy link
Author

snario commented Oct 3, 2019

I have a separate hacky workaround by the way:

const d = new Deferred();
const p1 = queue.add(() => { d.resolve(); ... });
const waitPromise = d.promise;

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

2 participants