import { parallel } from './parallel.js' import { validateConcurrency } from '@utils/validate-concurrency.js' import { map } from 'iterable-operator' import { go } from '@blackglory/go' import { Awaitable } from 'justypes' export function each( iterable: Iterable , fn: (element: T, i: number) => Awaitable , concurrency: number = Infinity ): Promise { validateConcurrency('concurrency', concurrency) return go(async () => { const tasks = map(iterable, (element, i) => () => fn(element, i)) return await parallel(tasks, concurrency) }) }