# sindresorhus/p-limit architecture

> Architecture diagram of the GitHub repository sindresorhus/p-limit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sindresorhus/p-limit
- Repository: https://github.com/sindresorhus/p-limit
- Video tour (about a minute): https://gitdiagram.com/sindresorhus/p-limit/video

## Overview

#### 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`](https://github.com/sindresorhus/p-limit/blob/main/index.js) (Runtime limiter API), [`index.d.ts`](https://github.com/sindresorhus/p-limit/blob/main/index.d.ts) (Type contract)

## Diagram (Mermaid)

```mermaid
flowchart TD

node_app(("Application code<br/>external consumer"))
node_runtime["Runtime limiter API<br/>Node.js/browser runtime<br/>[index.js]"]
node_types["Type contract<br/>TypeScript declarations<br/>[index.d.ts]"]

node_app -->|"supplies work and submits calls"| node_runtime
node_runtime -->|"returns promises and exposes API"| node_app
node_types -.->|"describes public surface"| node_runtime

click node_runtime "https://github.com/sindresorhus/p-limit/blob/main/index.js"
click node_types "https://github.com/sindresorhus/p-limit/blob/main/index.d.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_runtime,node_types toneNeutral
```

## Components

### Other components

- **Application code**: external consumer · Supplies async work and invokes limiter functions.
- [Runtime limiter API](https://github.com/sindresorhus/p-limit/blob/main/index.js): Node.js/browser runtime · Tracks concurrency, active executions, pending calls, and queue behavior. · `index.js`
- [Type contract](https://github.com/sindresorhus/p-limit/blob/main/index.d.ts): TypeScript declarations · Describes the exported limiter APIs and callable properties. · `index.d.ts`

## Connections

- 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
