AsyncPool
v0.0.9testedA concurrency-limited async task pool with AbortSignal support. Tasks start immediately when under the concurrency limit and are queued otherwise. Each task receives the pool's AbortSignal for cooperative cancellation. Use `all()` to wait for all tasks (throws AggregateError on any failure) or `allSettled()` to inspect individual results — mirroring the Promise static API.
Example
const pool = new AsyncPool({ concurrency: 3, signal: controller.signal });
for (const chunk of chunks) {
pool.add((signal) => fetch(chunk.url, { signal }));
}
await pool.all();Signature
class AsyncPoolProperties
| Property | Type | Description |
|---|---|---|
sizereadonly | number | — |
activereadonly | number | — |
concurrencyreadonly | number | — |
Methods
disposeDetaches the pool's abort listener from the provided signal. Call this when the pool is no longer needed but the signal outlives it (e.g. one long-lived controller feeding many pools), otherwise the pool stays reachable through the signal's listener list and cannot be GC'd.
dispose(): voidaddAdds a task to the pool. Starts immediately if under the concurrency limit; queues otherwise. The task receives the pool's AbortSignal for cooperative cancellation.
add<T>(task: (signal: AbortSignal) => Promise<T>): Promise<T>| Parameter | Type | Description |
|---|---|---|
task | (signal: AbortSignal) => Promise<T> | — |
Promise<T>allLike `Promise.all` — resolves when all tasks complete; throws `AggregateError` if any failed. Multiple concurrent callers share the same underlying promise.
async all(): Promise<void>Promise<void>allSettledLike `Promise.allSettled` — always resolves with the settled result of every task. Multiple concurrent callers share the same underlying promise.
allSettled(): Promise<Array<PromiseSettledResult<unknown>>>Promise<PromiseSettledResult<unknown>[]>