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 7/13/2026
Yes, you can drain the pending queue without running the tasks using the clearQueue() method. This discards all pending promises that are waiting to run.
From the README:
limit.clearQueue() — Discard pending promises that are waiting to run.
This might be useful if you want to tear down 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.
[readme.md:L96-L106]
Here's a simple example from the tests [test.js:L180-L188]:
const limit = pLimit(1);
Array.from({length: 1}, () => limit(() => delay(1000)));
Array.from({length: 3}, () => limit(() => delay(1000)));
await Promise.resolve();
t.is(limit.pendingCount, 3);
limit.clearQueue();
t.is(limit.pendingCount, 0);
Important note: When you call clearQueue(), the pending promises never settle by default. If you want pending promises to be rejected instead, you can enable the rejectOnClear option when creating the limiter:
const limit = pLimit({concurrency: 1, rejectOnClear: true});
This way, pending tasks will be rejected with an AbortError [test.js:L196-L210], which is useful if you're awaiting the promises with Promise.all so they don't remain unresolved.
FOLLOW_UPS:
- How do I handle graceful shutdown with clearQueue?
- What's the difference between activeCount and pendingCount?
- Can I adjust concurrency during execution?
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.