11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst AggregateError = require('aggregate-error');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cimodule.exports = async (
51cb0ef41Sopenharmony_ci	iterable,
61cb0ef41Sopenharmony_ci	mapper,
71cb0ef41Sopenharmony_ci	{
81cb0ef41Sopenharmony_ci		concurrency = Infinity,
91cb0ef41Sopenharmony_ci		stopOnError = true
101cb0ef41Sopenharmony_ci	} = {}
111cb0ef41Sopenharmony_ci) => {
121cb0ef41Sopenharmony_ci	return new Promise((resolve, reject) => {
131cb0ef41Sopenharmony_ci		if (typeof mapper !== 'function') {
141cb0ef41Sopenharmony_ci			throw new TypeError('Mapper function is required');
151cb0ef41Sopenharmony_ci		}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci		if (!((Number.isSafeInteger(concurrency) || concurrency === Infinity) && concurrency >= 1)) {
181cb0ef41Sopenharmony_ci			throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
191cb0ef41Sopenharmony_ci		}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci		const result = [];
221cb0ef41Sopenharmony_ci		const errors = [];
231cb0ef41Sopenharmony_ci		const iterator = iterable[Symbol.iterator]();
241cb0ef41Sopenharmony_ci		let isRejected = false;
251cb0ef41Sopenharmony_ci		let isIterableDone = false;
261cb0ef41Sopenharmony_ci		let resolvingCount = 0;
271cb0ef41Sopenharmony_ci		let currentIndex = 0;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci		const next = () => {
301cb0ef41Sopenharmony_ci			if (isRejected) {
311cb0ef41Sopenharmony_ci				return;
321cb0ef41Sopenharmony_ci			}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci			const nextItem = iterator.next();
351cb0ef41Sopenharmony_ci			const index = currentIndex;
361cb0ef41Sopenharmony_ci			currentIndex++;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci			if (nextItem.done) {
391cb0ef41Sopenharmony_ci				isIterableDone = true;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci				if (resolvingCount === 0) {
421cb0ef41Sopenharmony_ci					if (!stopOnError && errors.length !== 0) {
431cb0ef41Sopenharmony_ci						reject(new AggregateError(errors));
441cb0ef41Sopenharmony_ci					} else {
451cb0ef41Sopenharmony_ci						resolve(result);
461cb0ef41Sopenharmony_ci					}
471cb0ef41Sopenharmony_ci				}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci				return;
501cb0ef41Sopenharmony_ci			}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci			resolvingCount++;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci			(async () => {
551cb0ef41Sopenharmony_ci				try {
561cb0ef41Sopenharmony_ci					const element = await nextItem.value;
571cb0ef41Sopenharmony_ci					result[index] = await mapper(element, index);
581cb0ef41Sopenharmony_ci					resolvingCount--;
591cb0ef41Sopenharmony_ci					next();
601cb0ef41Sopenharmony_ci				} catch (error) {
611cb0ef41Sopenharmony_ci					if (stopOnError) {
621cb0ef41Sopenharmony_ci						isRejected = true;
631cb0ef41Sopenharmony_ci						reject(error);
641cb0ef41Sopenharmony_ci					} else {
651cb0ef41Sopenharmony_ci						errors.push(error);
661cb0ef41Sopenharmony_ci						resolvingCount--;
671cb0ef41Sopenharmony_ci						next();
681cb0ef41Sopenharmony_ci					}
691cb0ef41Sopenharmony_ci				}
701cb0ef41Sopenharmony_ci			})();
711cb0ef41Sopenharmony_ci		};
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci		for (let i = 0; i < concurrency; i++) {
741cb0ef41Sopenharmony_ci			next();
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci			if (isIterableDone) {
771cb0ef41Sopenharmony_ci				break;
781cb0ef41Sopenharmony_ci			}
791cb0ef41Sopenharmony_ci		}
801cb0ef41Sopenharmony_ci	});
811cb0ef41Sopenharmony_ci};
82