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

Add idle event #109

Merged
merged 5 commits into from
May 3, 2020
Merged

Add idle event #109

merged 5 commits into from
May 3, 2020

Conversation

Ilaiwi
Copy link
Contributor

@Ilaiwi Ilaiwi commented Apr 17, 2020

following #107

I added an idle event

idle

Emitted when the queue becomes empty, and all promises have completed; queue.size === 0 && queue.pending === 0.

const delay = require('delay');
const {default: PQueue} = require('p-queue');

const queue = new PQueue();

queue.on('idle', () => {
	console.log(`Queue is idle.  Size: ${queue.size}  Pending: ${queue.pending}`);
});

const job1 = queue.add(() => delay(2000));
const job2 = queue.add(() => delay(500));

await job1;
await job2;
// => 'Queue is idle.  Size: 0  Pending: 0'

await queue.add(() => delay(600));
// => 'Queue is idle.  Size: 0  Pending: 0'

@Ilaiwi Ilaiwi mentioned this pull request Apr 17, 2020
readme.md Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title #107 add idle event Add idle event Apr 27, 2020
Ilaiwi and others added 2 commits April 30, 2020 16:49
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@Ilaiwi
Copy link
Contributor Author

Ilaiwi commented Apr 30, 2020

@sindresorhus PR updated

@rightaway
Copy link

What's the difference between onIdle() and the idle event? There's also onEmpty which doesn't have an event equivalent. When to use the function vs event?

@sindresorhus
Copy link
Owner

onIdle resolves only once after being called, while idle is called every time the queue is idle.

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

Successfully merging this pull request may close these issues.

3 participants