function chunk ( list , chunkSize = 10000 ) { return new Array ( Math . ceil ( list . length / chunkSize ) ) . fill ( 0 ) . map ( ( _ , i ) => list . slice ( i * chunkSize , i * chunkSize + chunkSize ) ) ; } export async function chunkWork < T , TResult = T > ( items , max = [ 250000 , 10000 ] , work ) { return chunkSequencial ( items , max [ 0 ] , items => chunkParallel ( items , max [ 1 ] , work ) ) ; } export async function chunkSequencial < T , TResult = T > ( items , max = 1000 , work ) { let results = [ ] ; await chunk ( items , max ) . reduce ( ( state , items ) => state . then ( ( ) => work ( items ) . then ( result => ( results = results . concat ( result ) ) ) ) , Promise . resolve ( ) ) ; return results ; } export async function chunkParallel < T , TResult = T > ( items , max = 1000 , work ) { let results = [ ] ; const resultSet = await Promise . all ( chunk ( items , max ) . map ( items => work ( items ) ) ) ; resultSet . forEach ( result => ( results = results . concat ( result ) ) ) ; return results ; }