import { from } from 'rxjs' ; import { toArray } from 'rxjs/operators' ; import { lastValueFrom } from '../rxjs_7' ; import { IterableInput , AsyncMapFn , AsyncMapResult } from './types' ; import { mapWithLimit$ } from './observable' ; const getAllResults = < T > ( input ) => lastValueFrom ( from ( input ) . pipe ( toArray ( ) ) ) ; param iterable param fn export async function asyncMap < T1 , T2 > ( iterable , fn ) { return await asyncMapWithLimit ( iterable , Infinity , fn ) ; } param iterable param limit param fn export async function asyncMapWithLimit < T1 , T2 > ( iterable , limit , fn ) { const results$ = mapWithLimit$ ( iterable , limit , async ( item , i ) => [ i , await getAllResults ( fn ( item , i ) ) ] as const ) ; const results = await getAllResults ( results$ ) ; return results . sort ( ( [ a ] , [ b ] ) => a - b ) . reduce ( ( acc , [ , result ] ) => acc . concat ( result ) , [ ] ) ; }