Skip to content

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
@sindresorhus sindresorhus changed the title #107 add idle event Add idle event Apr 27, 2020
@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