Skip to main content

parallel(tasks, options?)

Run an array of inline functions concurrently. Returns results in input order.

function parallel<T>(
tasks: ReadonlyArray<() => T | Promise<T>>,
options?: ParallelOptions,
): Promise<T[]>;

Each function runs in its own isolated worker. Tasks must be self-contained.

const [a, b] = await parallel([() => doA(), () => doB()], { concurrency: 2 });

mapParallel(items, task, options?)

Parallel-map an iterable through a single task using a worker pool that's set up and torn down for you.

function mapParallel<TArg, TResult>(
items: ReadonlyArray<TArg>,
task: (arg: TArg) => TResult | Promise<TResult>,
options?: ParallelOptions,
): Promise<TResult[]>;

Far more efficient than parallel when running the same operation across many inputs — the pool is reused for every element.

const out = await mapParallel(urls, fetchAndParse, { concurrency: 8 });

ParallelOptions

interface ParallelOptions {
concurrency?: number; // worker count; default: availableParallelism()
timeout?: number; // per-task timeout (ms)
signal?: AbortSignal; // cancellation
}