Can I drain the pending queue without running them?
Can I drain the pending queue without running them?
About sindresorhus/p-limit · 2 messages · last activity 5/18/2026
Yes, you can drain the pending queue without running the tasks by calling limit.clearQueue(). This discards all pending promises that haven't started running yet.
From the README [readme.md:L96-L106]:
Discard pending promises that are waiting to run.
This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
Note: This does not cancel promises that are already running.
The implementation in [index.js:L86-L97] shows how it works:
clearQueue: {
value() {
if (!rejectOnClear) {
queue.clear();
return;
}
const abortError = AbortSignal.abort().reason;
while (queue.size > 0) {
queue.dequeue().reject(abortError);
}
},
},
Important note: By default, pending promises are simply discarded and never settle. If you want pending promises to be explicitly rejected when cleared, you can enable the rejectOnClear option when creating the limiter [index.d.ts:L109-L115]:
const limit = pLimit({concurrency: 1, rejectOnClear: true});
This is useful if you await the returned promises with Promise.all, so tasks don't remain unresolved after clearQueue() is called.
FOLLOW_UPS:
- What happens to currently running tasks after clearQueue()?
- How do I enable automatic rejection when clearing the queue?
- Can I drain the queue and check what was discarded?
Want to ask your own question?
Open chat for sindresorhus/p-limit →Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/p-limit. AI answers can be incomplete or stale — verify before relying on them.