How sindresorhus/p-limit works
Overview
p-limit is a small Node.js/browser library for limiting concurrent execution of promise-returning or async functions. It manages pending calls and exposes execution counts, queue clearing, and a mutable concurrency limit.
Components
• Runtime limiter and public API — index.js
Main implementation area for the default pLimit(concurrency) export and named limitFunction(fn, options) export. It owns per-limiter runtime state: active executions, pending calls, configured concurrency, and queue-clearing behavior. The public callable supports argument forwarding, map, activeCount, pendingCount, clearQueue(), and concurrency.
• Type contract — index.d.ts
Type declarations for the exported limiter APIs, options, callable properties, and promise-returning functions.
External Actors and Interfaces
Application code supplies async or promise-returning functions and may pass arguments through the limiter. It invokes the returned limiter functions and typically awaits the resulting promises or combines them with Promise.all.
The default export coordinates concurrency across calls made through one limiter. limitFunction instead returns a reusable limited function whose calls share that function’s configured concurrency. limit.map is the documented batch-processing convenience interface.
Relationships
The application supplies work functions → pLimit or limitFunction creates a limiter → caller submits calls → the runtime tracks active and pending work and starts calls subject to the configured limit → returned promises provide each operation’s result or rejection.
clearQueue() affects only pending calls; documented running operations continue. With rejectOnClear, discarded pending promises reject with AbortError; otherwise they remain unresolved. The type declarations describe the same public runtime surface but do not own execution state.
State and Boundaries
State is scoped to limiter instances and consists of concurrency configuration, active-count information, and pending queued calls. The documented package surface contains no persistent-storage or external-service subsystem. It limits scheduling only; cancellation of already-running application work is outside its boundary.
Main entry points: index.js Runtime limiter API · index.d.ts Type contract
Components 3
Other components
- Application code · Supplies async work and invokes limiter functions.
- Runtime limiter API · Tracks concurrency, active executions, pending calls, and queue behavior.
index.js - Type contract · Describes the exported limiter APIs and callable properties.
index.d.ts
Connections 3
- Application code → Runtime limiter API: supplies work and submits calls
- Runtime limiter API → Application code: returns promises and exposes API
- Type contract → Runtime limiter API: describes public surface