# minimal-promise-pool

> A minimal library for managing multiple promise instances (promise pool).

Latest version **6.0.3** (published 2026-07-03) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install minimal-promise-pool
pnpm add minimal-promise-pool
yarn add minimal-promise-pool
bun add minimal-promise-pool
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.0.3 |
| Published | 2026-07-03 |
| First published | 2021-10-09 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=24 |
| Dependencies | 0 |
| Unpacked size | 40.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 2 |
| Author | WillBooster Inc. |
| Maintainers | exkazuu |
| Keywords | async, promise, promise-pool |

## Links

- npm: https://www.npmjs.com/package/minimal-promise-pool
- Repository: https://github.com/WillBooster/minimal-promise-pool
- Homepage: https://github.com/WillBooster/minimal-promise-pool#readme
- Issues: https://github.com/WillBooster/minimal-promise-pool/issues
- npm.io page: https://npm.io/package/minimal-promise-pool

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 6.0.3 (latest) — 2026-07-03
- 6.0.2 — 2026-04-18
- 6.0.1 — 2025-12-06
- 6.0.0 — 2025-11-05
- 5.0.0 — 2025-06-07
- 4.1.3 — 2025-06-07
- 4.1.2 — 2024-10-20
- 4.1.1 — 2024-06-23
- 4.1.0 — 2024-06-23
- 4.0.0 — 2023-09-14
- 3.0.3 — 2023-08-26
- 3.0.2 — 2023-08-11
- 3.0.1 — 2023-07-10
- 3.0.0 — 2023-07-10
- 2.1.13 — 2023-06-18
- … 22 more at https://npm.io/package/minimal-promise-pool/versions

## README

# minimal-promise-pool

[![Test](https://github.com/WillBooster/minimal-promise-pool/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/minimal-promise-pool/actions/workflows/test.yml)
[![npm version](https://img.shields.io/npm/v/minimal-promise-pool.svg)](https://www.npmjs.com/package/minimal-promise-pool)
[![license](https://img.shields.io/npm/l/minimal-promise-pool.svg)](https://github.com/WillBooster/minimal-promise-pool/blob/main/LICENSE)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

A minimal, zero-dependency promise pool for limiting the number of concurrently running promises.
For example, `new PromisePool(2)` runs at most two tasks at the same time and queues the rest.

## Features

- **Minimal** — a single class with no runtime dependencies.
- **Typed** — written in TypeScript with full type definitions.
- **Dual package** — ships both ESM and CommonJS builds.
- **FIFO scheduling** — queued tasks start in the order they were submitted.
- **Adjustable concurrency** — change the limit at runtime; the pool adapts immediately.

## Installation

```sh
npm install minimal-promise-pool
# or
yarn add minimal-promise-pool
```

## Quick Start

The following example runs at most two tasks concurrently:

```ts
import { PromisePool } from 'minimal-promise-pool';

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const promisePool = new PromisePool(2);
await promisePool.run(async () => {
  console.log('First task started');
  await sleep(10_000);
  console.log('First task finished');
});
await promisePool.run(async () => {
  console.log('Second task started');
  await sleep(10_000);
  console.log('Second task finished');
});
await promisePool.run(async () => {
  console.log('Third task started');
  await sleep(10_000);
  console.log('Third task finished');
});
```

Output:

```
First task started
Second task started
# ... about 10 seconds ...
First task finished
Third task started
Second task finished
# ... about 10 seconds ...
Third task finished
```

Note that `run()` resolves when the task **starts**, not when it finishes.
`await promisePool.run(...)` therefore applies backpressure: it pauses the caller only while the pool is full.

## Usage

### Getting a task's return value

Use `runAndWaitForReturnValue()` when you need the task's result (or its error):

```ts
const promisePool = new PromisePool(5);

const results = await Promise.all(
  urls.map((url) => promisePool.runAndWaitForReturnValue(async () => (await fetch(url)).json()))
);
```

### Waiting for all running tasks

```ts
// Waits for all currently running tasks; rejects if any of them fails.
await promisePool.promiseAll();

// Waits for all currently running tasks and collects each outcome.
const outcomes = await promisePool.promiseAllSettled();
```

### Adjusting concurrency at runtime

```ts
const promisePool = new PromisePool(10);
promisePool.concurrency = 2; // Running tasks continue; new tasks respect the new limit.
promisePool.concurrency = 20; // Queued tasks start immediately up to the new limit.
```

## API

### `new PromisePool<T>(concurrency = 10)`

Creates a pool that runs at most `concurrency` tasks concurrently.

### Methods

| Method                                   | Returns                              | Description                                                                               |
| ---------------------------------------- | ------------------------------------ | ----------------------------------------------------------------------------------------- |
| `run(startPromise)`                      | `Promise<void>`                      | Runs the task when the pool has capacity. Resolves once the task has started.             |
| `runAndWaitForReturnValue(startPromise)` | `Promise<R>`                         | Like `run()`, but resolves with the task's return value (and rejects if the task throws). |
| `promiseAll()`                           | `Promise<T[]>`                       | `Promise.all()` over the currently running tasks.                                         |
| `promiseAllSettled()`                    | `Promise<PromiseSettledResult<T>[]>` | `Promise.allSettled()` over the currently running tasks.                                  |

### Properties

| Property              | Type     | Description                                                                         |
| --------------------- | -------- | ----------------------------------------------------------------------------------- |
| `concurrency`         | `number` | The maximum number of concurrent tasks. Writable; increasing it wakes queued tasks. |
| `workingPromiseCount` | `number` | The number of currently running tasks.                                              |
| `queuedPromiseCount`  | `number` | The number of tasks that have been submitted but not yet finished.                  |

### Error handling

A rejection from a task passed to `run()` is not reported through `run()`'s returned promise (which only signals that the task started), and it becomes an unhandled promise rejection unless something else observes it.
`promiseAll()` and `promiseAllSettled()` cover only the tasks still running at the moment of the call — a task that has already settled is removed from the pool, so a later call cannot collect its rejection.

When you need task outcomes reliably, use `runAndWaitForReturnValue()` and collect the returned promises yourself:

```ts
const outcomes = await Promise.allSettled(tasks.map((task) => promisePool.runAndWaitForReturnValue(task)));
```

## License

Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).

---
_Source: https://npm.io/package/minimal-promise-pool · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
