import { envNumber } from "./env.ts"; const limitEnv = envNumber("LUME_CONCURRENCY"); const limitDefault = limitEnv !== undefined && limitEnv >= 1 ? limitEnv : 200; /** Run a callback concurrently with all the elements of an Iterable */ export async function concurrent( iterable: AsyncIterable | Iterable, iteratorFn: (arg: Type) => Promise, limit = limitDefault, ) { const executing: Promise[] = []; for await (const item of iterable) { const p: Promise = iteratorFn(item).then(() => executing.splice(executing.indexOf(p), 1) ); executing.push(p); if (executing.length >= limit) { await Promise.race(executing); } } await Promise.all(executing); }