UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

38 lines 1.84 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import toIterator from './toIterator'; import toArray from './toArray'; /** Calls `Promise.all` on all collected values, optionally limiting concurrent in-flight promises. */ export function promiseAll(arg_1) { return __awaiter(this, arguments, void 0, function* (arg, { concurrency = Infinity } = {}) { if (concurrency <= 0) throw new Error('Concurrency must be positive'); if (concurrency === Infinity) return Promise.all(toArray(arg)); const results = []; const executing = new Set(); const iterator = toIterator(arg); let next; while (!(next = iterator.next()).done) { const promise = next.value; results.push(promise); const executingPromise = Promise.resolve(promise).finally(() => { executing.delete(executingPromise); }); executing.add(executingPromise); if (executing.size >= concurrency) { yield Promise.race(executing); } } return Promise.all(results); }); } export default promiseAll; //# sourceMappingURL=promiseAll.js.map