RepoPilot

Can I drain the pending queue without running them?

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

Question

Can I drain the pending queue without running them?

RepoPilot

Yes, you can drain the pending queue without running the pending tasks using the clearQueue() method. This discards all pending promises that are waiting to run.

According to the documentation in [readme.md:L96-L106], limit.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.

The test in [test.js:L180-L188] demonstrates this behavior:

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

This shows that after calling clearQueue(), the pendingCount drops to 0, meaning all pending tasks were discarded without execution.

Optional behavior: If you want pending promises to be explicitly rejected instead of silently discarded, you can enable the rejectOnClear option when creating the limiter [index.d.ts:L115-L121]. This is useful if you're awaiting the promises with Promise.all() so they don't remain unresolved.

FOLLOW_UPS:

  • How does rejectOnClear differ from normal clearQueue()?
  • What happens to currently running promises with clearQueue()?
  • Can I check pending count before clearing?

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.