RepoPilot

Can I drain the pending queue without running them?

About sindresorhus/p-limit · 2 messages · last activity 7/27/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.

From [index.d.ts:L18-L27], the clearQueue() function is documented as:

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.

So when you call limit.clearQueue(), it will:

  • Remove all pending tasks from the queue (tasks that haven't started yet)
  • Leave already-running tasks untouched
  • Not execute those pending tasks

By default, pending promises simply don't settle. However, if you want pending promises to be rejected instead, you can enable the rejectOnClear option when creating the limiter. From [index.d.ts:L114-L122], when rejectOnClear is true, pending promises are rejected with an AbortError, which is useful if you're awaiting the returned promises with Promise.all() so they don't remain unresolved after clearing.

Example from [recipes.md:L89-L91]:

limit.clearQueue();
// Discard pending tasks (already running tasks will complete)

FOLLOW_UPS:

  • How do I enable rejectOnClear when creating a limiter?
  • What happens to already-running promises with clearQueue?
  • How do I track which tasks are pending vs. active?

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.