async-map-in-batches
v1.3.0
Published
Map an array to another with batched async operations.
Downloads
5
Readme
Async Map In Batches
The Problem
Your code needs to do a lot of IO work to map some input to some other output after some async processing.
This Solution
Iterate over an array and map it to a new one with an async iterator in batches.
Install
yarn add async-map-in-batches
Usage
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}]
API
Input
- 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
Output
outputArray: Array
Signature
async function asyncMapInChunks<T, V>(
array: Array<T>,
asyncMap: AsyncMapIterator<T, V>,
batchSize: number = 20,
onBatch: (batchNumber: number, batchCount: number) => void = () => {}
): Promise<Array<V>>;