You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `idle` event is emitted every time the queue reaches an idle state. On the other hand, the promise the `onIdle()` function returns resolves once the queue becomes idle instead of every time the queue is idle.
247
247
248
+
#### add
249
+
250
+
Emitted every time the add method is called and the number of pending or queued tasks is increased.
251
+
252
+
#### next
253
+
254
+
Emitted every time a task is completed and the number of pending or queued tasks is decreased.
255
+
256
+
```js
257
+
constdelay=require('delay');
258
+
const {default:PQueue} =require('p-queue');
259
+
260
+
constqueue=newPQueue();
261
+
262
+
queue.on('add', () => {
263
+
console.log(`Task is added. Size: ${queue.size} Pending: ${queue.pending}`);
264
+
});
265
+
queue.on('next', () => {
266
+
console.log(`Task is completed. Size: ${queue.size} Pending: ${queue.pending}`);
267
+
});
268
+
269
+
constjob1=queue.add(() =>delay(2000));
270
+
constjob2=queue.add(() =>delay(500));
271
+
272
+
await job1;
273
+
await job2;
274
+
// => 'Task is added. Size: 0 Pending: 1'
275
+
// => 'Task is added. Size: 0 Pending: 2'
276
+
277
+
awaitqueue.add(() =>delay(600));
278
+
// => 'Task is completed. Size: 0 Pending: 1'
279
+
// => 'Task is completed. Size: 0 Pending: 0'
280
+
```
281
+
248
282
## Advanced example
249
283
250
284
A more advanced example to help you understand the flow.
0 commit comments