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 8/1/2026
Yes, you can drain the pending queue without running them using the clearQueue() method. This discards all pending promises that are waiting to run.
According to [index.d.ts:L18-L26], clearQueue() will:
- Discard pending promises that are waiting to run
- Not cancel promises that are already running
- Optionally reject pending promises with an
AbortErrorifrejectOnClearis enabled
Here's how it works:
Basic usage (pending promises never settle):
limit.clearQueue();
With rejectOnClear option (pending promises are rejected):
const limit = pLimit({concurrency: 1, rejectOnClear: true});
limit.clearQueue(); // Pending promises reject with AbortError
The test in [test.js:L186-L192] shows that after calling clearQueue(), the pendingCount immediately drops to 0:
Array.from({length: 3}, () => limit(() => delay(1000)));
await Promise.resolve();
t.is(limit.pendingCount, 3);
limit.clearQueue();
t.is(limit.pendingCount, 0);
Important note: Without rejectOnClear: true, pending promises will never settle, so you won't be able to await them. The [recipes.md:L76-L115] example demonstrates using clearQueue() during graceful shutdown, where it clears pending tasks while only waiting for already-running tasks to complete.
FOLLOW_UPS:
- What happens to running promises when clearQueue is called?
- How does rejectOnClear option change the behavior?
- What's the difference between activeCount and pendingCount?
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.