/** Run a callback concurrently with all the elements of an Iterable */ export async function concurrent( iterable: AsyncIterable | Iterable, iteratorFn: (arg: Type) => Promise, limit = 200, ) { 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); }