RepoPilot

Can I drain the pending queue without running them?

About sindresorhus/p-limit · 2 messages · last activity 6/29/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 documentation in [readme.md:L96-L106], clearQueue() discards pending promises that are waiting to run. This is useful if you want to tear down the queue at the end of your program's lifecycle or discard function calls.

Important caveat: This does not cancel promises that are already running—only the pending (queued) ones are discarded.

Example from the test suite:

[test.js:L180-L185] shows how clearQueue() works:

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);

After calling clearQueue(), the pendingCount drops to 0—the queued tasks are discarded without execution.

With rejectOnClear:

If you want pending tasks to be rejected with an AbortError instead of silently discarded, you can enable the rejectOnClear option [index.d.ts:L105-L108]:

const limit = pLimit({concurrency: 1, rejectOnClear: true});
limit.clearQueue(); // pending promises reject with AbortError

This is recommended when you await the returned promises (e.g., with Promise.all) so tasks don't remain unresolved.

FOLLOW_UPS:

  • How does clearQueue() interact with running tasks?
  • What is the difference between activeCount and pendingCount?
  • Can I get notified when a task is 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.