Skip to content

parallel

Runs an async mapper over an array with a concurrency limit (worker-pool semaphore). Results preserve input order and follow the Promise.allSettled shape — fn errors never reject the call.

import { parallel } from "1o1-utils";
import { parallel } from "1o1-utils/parallel";
async function parallel<T, R>({
items,
concurrency,
fn,
signal,
}: ParallelParams<T, R>): Promise<Array<ParallelSettledResult<R>>>
NameTypeRequiredDescription
itemsreadonly T[]YesItems to process
concurrencynumberYesMax in-flight tasks (positive integer)
fn(item: T, index: number) => Promise<R> | RYesMapper invoked per item; may be sync or async
signalAbortSignalNoCancels pending work; in-flight tasks finish normally

Promise<Array<{ status: "fulfilled"; value: R } | { status: "rejected"; reason: unknown }>> — entries in input order.

const results = await parallel({
items: urls,
concurrency: 3,
fn: async (url) => (await fetch(url)).json(),
});
for (const r of results) {
if (r.status === "fulfilled") console.log(r.value);
else console.error(r.reason);
}
// With AbortSignal
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);
await parallel({
items,
concurrency: 5,
signal: controller.signal,
fn: doWork,
});
  • Empty items resolves to [].
  • concurrency >= items.length runs everything in parallel.
  • concurrency: 1 runs serially.
  • Errors thrown by fn are captured as { status: "rejected", reason } — the call itself never rejects from them.
  • Throws if signal is already aborted at call time.
  • Once signal aborts, unstarted indices resolve as rejected with signal.reason; in-flight tasks complete and their results are recorded.
  • Throws if items is not an array, concurrency is not a positive integer, fn is not a function, or signal is not an AbortSignal.

concurrency limit, semaphore, worker pool, async map, throttle parallel

I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use parallel to fetch 100 URLs with a concurrency cap of 5