p-limit
Tiny promise concurrency limiter that runs at most N tasks at once while keeping results and errors in order.
Official Documentation ↗v7.3.3. Check the official docs for the latest API surface.Installation
npm install p-limitpnpm add p-limitMethods
No methods match this filter.
Tiny promise concurrency limiter that runs at most N tasks at once while keeping results and errors in order.
Overview
p-limit is the smallest useful concurrency primitive: wrap a thunk, and at most N thunks run concurrently while the remainder wait in a FIFO queue. It composes naturally with Promise.all, allSettled, or manual bookkeeping.
Because the limit function returns the promise of the wrapped task, call sites stay unchanged, mapping a list through limit(fn) looks identical to mapping through fn. activeCount and pendingCount make backlogs observable, and clearQueue gives operators a stop button for shutdown paths.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
pLimit— Creates a limiter that allows at most concurrency simultaneously active tasks, queueing the rest.activeCount / pendingCount— Exposes how many wrapped tasks are running right now and how many are still waiting in the queue.clearQueue— Discards every queued, not yet started task; their promises reject with a cancelation-style error.
Usage Notes
- Choose concurrency from the downstream budget: database pools, API rate limits, and CPU core count each suggest a different N.
- Wrap the whole task, including any setup, inside limit(() => …) so slot occupancy reflects true cost.
- Combine with Promise.allSettled to collect partial failures instead of abandoning the remaining queue on the first error.
Common Pitfalls
- Creating the limit inside a loop gives every task its own unlimited limiter; create it once and share it.
- Waiting on the limiter is not cancelation: tasks already running cannot be aborted by p-limit itself.
- A concurrency of Infinity is the same as no limit; guard the constructor argument when it comes from configuration.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/sindresorhus/p-limit@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:28.398Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- If the iterable throws during iteration, the returned promise rejects with that error. (https://cdn.jsdelivr.net/gh/sindresorhus/p-limit@latest/readme.md)
FAQ
- How is p-limit different from p-queue?
- p-limit answers one question: at most N tasks at once. p-queue adds intervals, priorities, pausing, and event emitters. If you only need a concurrency cap, p-limit is smaller and has no state beyond the queue itself.
- Do errors break the limit?
- A rejected task frees its slot immediately; the queue keeps draining. With Promise.all the first rejection aborts your own aggregation, so use Promise.allSettled when you want every task to finish.
- Why is my code still slow with a limit?
- The limit only controls fan-out. If each task opens a fresh connection, the connection handshake dominates. Combine p-limit with a shared agent or session, and keep tasks chunky rather than one limit call per row.
Report a problem with this API docOpens a pre-filled issue on GitHub.