/** * Execute queue of promises in a streaming fashion. * * Optimized for streaming: * - Expects an iterable as input * - Does not return a list of all results * * Inspired by github.com/rxaviers/async-pool */ export async function promisesQueueStreaming( factories: AsyncIterable<() => Promise> | Iterable<() => Promise>, concurrency: number ): Promise { const executing: Promise[] = []; for await (const factory of factories) { const e = factory().then(() => { executing.splice(executing.indexOf(e), 1); }); executing.push(e); if (executing.length >= concurrency) { await Promise.race(executing); } } await Promise.all(executing); }