v10.0.0
Added 1
- A task with a timeout now receives a signal that aborts when the task times out, so it can be passed to fetch() and similar to stop the work
Changed 14
- The queue is now an EventTarget instead of an EventEmitter, so use addEventListener() and removeEventListener() instead of on(), once(), off(), and other EventEmitter methods behavior changed · vendor-flagged, marked in the notes below
- The completed and error events now carry the result or the error in event.detail
- An error thrown in an event listener now exits the process as an uncaught exception
- clear() now rejects the promises of removed tasks with an AbortError instead of leaving them pending forever
- TimeoutError is now a DOMException named TimeoutError, like the error of AbortSignal.timeout() behavior changed · vendor-flagged, marked in the notes below
- concurrency and intervalCap must now be integers or Infinity, so a limit like 1.5 no longer lets 2 tasks run behavior changed · vendor-flagged, marked in the notes below
- timeout and interval values above 2147483647 ms are now rejected instead of firing after 1 ms behavior changed · vendor-flagged, marked in the notes below
- add() now rejects with a TypeError when priority is NaN or not a number behavior changed · vendor-flagged, marked in the notes below
- start(), clear(), and setting concurrency no longer emit empty and idle when the queue is already empty, so idle no longer fires on a queue that never had a task
- A custom queueClass must now implement remove() signature changed · vendor-flagged, marked in the notes below
- The library now has zero dependencies
- A task now runs in the async context of its add() call, so it sees the AsyncLocalStorage values of the caller even when it starts later (Node.js 22.3 and later, Deno, and Bun) requirement raised · vendor-flagged, marked in the notes below
- onEmpty() and the empty event now fire when the last waiting task starts, not when the next task finishes
- addAll() now resolves with a typed tuple when the tasks return different types
Fixed 5
- Many rate-limit bugs were fixed where intervalCap was exceeded at the end of a window, a waiting task got stuck, or isRateLimited was wrong
- Slow add, run, and abort for many tasks sharing a signal was fixed, and PriorityQueue no longer keeps finished tasks in memory
- Many edge cases with events and priorities were fixed, including infinite recursion in an idle listener and events firing in the wrong order
- undefined options and an invalid timeout setter no longer break the queue
- Many TypeScript types were fixed
Removed 2
- Node.js 22 is now required
- The deprecated carryoverConcurrencyCount option was removed; use carryoverIntervalCount instead vendor-flagged, marked in the notes below
11 changes to check, marked in the notes below
From p-queue
Breaking
- Require Node.js 22 300a52d requirement raised · vendor-flagged
- The queue is now an
EventTargetinstead of anEventEmitter300a52d behavior changed · vendor-flagged- Use
.addEventListener()and.removeEventListener()instead of.on(),.once(),.off(), and the otherEventEmittermethods. - The
completedanderrorevents have the result or the error inevent.detail. - An error thrown in an event listener now exits the process. Node.js reports it as an uncaught exception.
- Use
.clear()now rejects the promises of the removed tasks with anAbortErrorinstead of leaving them pending forever 64311ff behavior changed · vendor-flagged.clear()marks these promises as handled, but a promise made from them, like the one from.addAll()orqueue.add(fn).then(…), now rejects, so handle it.
TimeoutErroris now aDOMExceptionnamedTimeoutError, like the error ofAbortSignal.timeout()300a52d behavior changed · vendor-flagged- Remove the deprecated
carryoverConcurrencyCountoption: usecarryoverIntervalCount300a52d removed · vendor-flagged - Stricter validation of options 76a48ba d65d567
concurrencyandintervalCapmust be integers (orInfinity). A limit of1.5used to let 2 tasks run. behavior changed · vendor-flaggedtimeoutandintervalabove2147483647ms (the maximum timer delay) are rejected. They used to fire after 1 ms. behavior changed · vendor-flagged.add()rejects with aTypeErrorwhenpriorityisNaNor not a number. behavior changed · vendor-flagged
.start(),.clear(), and settingconcurrencyno longer emitemptyandidlewhen the queue is already empty, soidleno longer fires on a queue that never had a task aeedf53 behavior changed · vendor-flagged- A custom
queueClassmust now implementremove()300a52d signature changed · vendor-flagged
Improvements
- Zero dependencies! 300a52d
- A task now runs in the async context of its
.add()call, so it sees theAsyncLocalStoragevalues of the caller, also when it starts later (Node.js 22.3 and later, Deno, and Bun) (#200) a5b7b54- Previously, a waiting task could see the values of whatever started it, like another request on a server.
- A task with a
timeoutnow gets asignalthat aborts when the task times out, so you can pass it on tofetch()and similar to stop the work 300a52d .onEmpty()and theemptyevent now fire when the last waiting task starts, not when the next task finishes 1cf5b82 cb25a0a behavior changed · inferred.addAll()now resolves with a typed tuple when the tasks return different types 36e83aa
Fixes
- Fix many rate-limit bugs where
intervalCapwas exceeded at the end of a window, a waiting task got stuck, orisRateLimitedwas wrong 6af55a7 1cf5b82 6c08a4f ac89911 450356a - Fix slow add, run, and abort for many tasks that share a signal, and
PriorityQueuekeeping finished tasks in memory 95622a4 6fb8337 - Fix many edge cases with events and priorities, like infinite recursion in an
idlelistener and events in the wrong order aeedf53 b33dd44 8792846 e79f970 d65d567 6f6028a - Fix
undefinedoptions and an invalid.timeoutsetter breaking the queue d6cf96b 48586f4 - Fix many TypeScript types 36e83aa fa3c3e0
Migration guide
Events
-queue.on('completed', result => {
- console.log(result);
-});
+queue.addEventListener('completed', event => {
+ console.log(event.detail);
+});
-queue.once('idle', () => {});
+queue.addEventListener('idle', () => {}, {once: true});
If the result or the error is undefined, event.detail is null, because CustomEvent works that way.
Other differences from EventEmitter:
- If you add the same listener twice, it is called once.
- A listener that is removed while an event is dispatched is not called for that event.
.clear()
The promises of the removed tasks now reject with an AbortError. If you await them, handle the rejection:
const results = await queue.addAll(tasks).catch(error => {
if (error.name === 'AbortError') {
return [];
}
throw error;
});
Timeouts and signals
error instanceof TimeoutError still works. You can also check error.name === 'TimeoutError', which also matches the error of AbortSignal.timeout().
Other changes:
- The
TimeoutErrorstack trace no longer shows where the task was added. - With
timeoutset, the task'ssignalis no longer linked to yoursignalafter the task finishes. - Tasks with a
signalortimeoutsettle 2 microtasks later than before.
https://github.com/sindresorhus/p-queue/compare/v9.3.5...v10.0.0