1.3.0 • Published 5 years ago

async-map-in-batches v1.3.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Async Map In Batches

CircleCI NPM BundlePhobia

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>>;
1.3.0

5 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago