PQueue
Promise queue with concurrency caps, rate intervals, priorities, and lifecycle events for background job fan-out.
Official Documentation ↗v10.0.0. Check the official docs for the latest API surface.Installation
npm install p-queuepnpm add p-queueMethods
No methods match this filter.
Promise queue with concurrency caps, rate intervals, priorities, and lifecycle events for background job fan-out.
Overview
PQueue schedules promise-returning work under concurrency and rate constraints. add enqueues a task and returns its promise unchanged, so fan-out code stays promise-shaped while the queue meters execution underneath.
Beyond the cap it offers priorities, pause and resume, clear for shutdown, and events for observability. The interval mode turns it into a rate limiter for APIs with per-second quotas, and onIdle gives batch pipelines a clean completion barrier.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
new PQueue— Creates a queue; concurrency bounds parallel tasks, and interval plus intervalCap enforce an execution rate.add— Enqueues a task, optionally with priority where higher numbers run first, and returns its result promise.onIdle— Resolves when the queue has drained completely: no pending and no running tasks remain.pause / clear— Suspends task starts, resumes them, or discards waiting tasks without touching running ones.events— Signals queue state transitions: active when a task starts, idle when the queue empties, add and next around task flow.
Usage Notes
- Create one queue per downstream budget and share it; per-request queues serialize nothing and remember nothing.
- Give user-triggered tasks higher priority so maintenance scans cannot starve interactive work.
- Await onIdle before reporting batch success; Promise.all over add calls alone misses tasks still inside the queue.
Common Pitfalls
- clear() only drops waiting tasks: running promises still resolve or reject and need their own error handling.
- An unhandled rejection inside a queued task surfaces through the returned promise; attach catch even when results are ignored.
- intervalCap without carryoverConcurrencyCount can double the effective burst at interval boundaries.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/sindresorhus/p-queue@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:30.497Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- When the time is up, the signal of the task aborts and .add() rejects with a TimeoutError, which is a DOMException. (https://cdn.jsdelivr.net/gh/sindresorhus/p-queue@latest/readme.md)
- If it aborts while the task waits, the task is removed and .add() rejects with the abort reason. (https://cdn.jsdelivr.net/gh/sindresorhus/p-queue@latest/readme.md)
- If it aborts while the task runs, .add() rejects and the concurrency slot is freed, but the task keeps running unless it handles the signal itself. (https://cdn.jsdelivr.net/gh/sindresorhus/p-queue@latest/readme.md)
FAQ
- How do concurrency and intervalCap interact?
- concurrency caps how many tasks run at once; interval with intervalCap caps how many may start per interval. With carryoverConcurrencyCount, tasks started near the end of an interval count against the next one, which smooths bursty rates.
- What does queue.size versus queue.pending report?
- size counts tasks waiting to start; pending counts tasks currently running. onIdle resolves when both hit zero, which is the signal for batch completion.
- How do I stop a queue gracefully?
- Call clear() to drop waiting tasks, then await onIdle() so running tasks finish. The queue does not abort in-flight work itself; tasks must observe your own cancelation signal such as an AbortController.
Report a problem with this API docOpens a pre-filled issue on GitHub.