Your code needs to do a lot of IO work to map some input to some other output after some async processing.
Iterate over an array and map it to a new one with an async iterator in batches.
yarn add async-map-in-batches
import asyncMapInBatches from "async-map-in-batches";
// Given an array of elements
const inputArray = Array.from({ length: 100 }, (v, i) => i);
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const outputArray = await asyncMapInBatches(inputArray, async i => {
await delay(i);
return { i };
});
// [{i: 0}, {i: 1}...{i: 100}]
- inputArray: Array (required)
- asyncIterator: async (val:any, i:number) => V (required)
- batchSize: number (optional)
- onBatch: (batchNumber:number, batchCount: number) => void Called once per promise batch
outputArray: Array
async function asyncMapInChunks<T, V>(
array: Array<T>,
asyncMap: AsyncMapIterator<T, V>,
batchSize: number = 20,
onBatch: (batchNumber: number, batchCount: number) => void = () => {}
): Promise<Array<V>>;