RepoPilot

Can I drain the pending queue without running them?

About sindresorhus/p-limit · 2 messages · last activity 5/25/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. This discards all pending promises that are waiting to run.

According to [index.d.ts:L19-L27], clearQueue() will:

Discard pending promises that are waiting to run. This might be useful if you want to teardown 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.

Here's a practical example from the test file [test.js:L182-L190]:

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, meaning all pending tasks have been discarded without execution.

Important note: By default, pending promises never settle when cleared. However, if you enable the rejectOnClear option when creating the limiter, pending promises will be rejected with an AbortError [index.d.ts:L104-L108], which is recommended if you're awaiting the promises with Promise.all.

FOLLOW_UPS:

  • How does rejectOnClear change queue clearing behavior?
  • What's the difference between activeCount and pendingCount?
  • Can I modify the concurrency limit dynamically?

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.