RepoPilot

Can I drain the pending queue without running them?

About sindresorhus/p-limit · 2 messages · last activity 6/1/2026

Question

Can I drain the pending queue without running them?

RepoPilot

Yes, you can drain the pending queue without running the tasks using the clearQueue() method.

According to the API documentation in [index.d.ts:L16-L24], clearQueue() will "Discard pending promises that are waiting to run." This means any tasks in the queue that haven't started executing yet will be removed without being executed.

Here's what happens:

  • Pending tasks are discarded — their internal fn is never called
  • Already running tasks continueclearQueue() does not cancel promises that are already executing
  • Optional rejection — if you initialize the limiter with rejectOnClear: true, pending promises will be rejected with an AbortError instead of just being silently discarded

Example from the test suite [test.js:L183-L191]:

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);  // 3 tasks waiting
limit.clearQueue();
t.is(limit.pendingCount, 0);  // All pending tasks removed

If you need pending promises to be properly rejected (recommended when using Promise.all), use the rejectOnClear option as shown in [test.js:L197-L211].

FOLLOW_UPS:

  • How do I enable rejection when clearing the queue?
  • What's the difference between activeCount and pendingCount?
  • Can I adjust concurrency while tasks are running?

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.