import * as Rx from 'rxjs' ; import { mergeMap , toArray , map } from 'rxjs/operators' ; import { lastValueFrom } from '@kbn/std' ; export async function concurrentMap < T , T2 > ( concurrency , arr , fn ) { if ( ! arr . length ) { return [ ] ; } return await lastValueFrom ( Rx . from ( arr ) . pipe ( mergeMap ( async ( item , index ) => ( { index , result : await fn ( item , index ) } ) , concurrency ) , toArray ( ) , map ( ( list ) => list . sort ( ( a , b ) => a . index - b . index ) . map ( ( { result } ) => result ) ) ) ) ; }