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 9/1/2026
Yes, you can drain the pending queue without running the pending tasks using the clearQueue() method [index.d.ts:L20-L28].
When you call limit.clearQueue(), it discards all pending promises that are waiting to run without executing them [readme.md:L96-L99]. The method specifically states: "Note: This does not cancel promises that are already running" [readme.md:L102].
By default, when you clear the queue, pending promises are simply discarded and never settle. However, if you want pending tasks to be explicitly rejected instead (to avoid promises hanging indefinitely), you can enable the rejectOnClear option when creating the limiter:
const limit = pLimit({concurrency: 1, rejectOnClear: true});
With rejectOnClear: true, pending promises are rejected with an AbortError when clearQueue() is called [index.d.ts:L26-L28], which is recommended if you're using Promise.all() or similar constructs.
The implementation shows that clearQueue() either silently clears the queue or rejects each pending item with an abort error, depending on the rejectOnClear setting [index.js:L84-L95].
FOLLOW_UPS:
- How does clearQueue() interact with already running tasks?
- What's the difference between rejectOnClear true and false?
- Can I customize the error type for rejected pending tasks?
Want to ask your own question?
Open chat for sindresorhus/p-limitPublic 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.