Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/morishin/promises-in-batches

Execute promises sequentially in batches.
https://github.com/morishin/promises-in-batches

Last synced: 23 days ago
JSON representation

Execute promises sequentially in batches.

Awesome Lists containing this project

README

        

[![CircleCI](https://circleci.com/gh/morishin/promises-in-batches.svg?style=svg)](https://circleci.com/gh/morishin/promises-in-batches)

# promises-in-batches

Execute promises sequentially in batches.

You can executes promises concurrently with a limitation of concurrency.

e.g.) Batch Size: 3

![promises-in-batches](https://user-images.githubusercontent.com/1413408/52653624-a7689f80-2f33-11e9-91cf-4064f9a3974b.png)

## Installation

```shell
npm install promises-in-batches
```

## Usage

```typescript
import { makePromiseBatch } from "promises-in-batches";

const promiseGenerator1: () => Promise = () =>
new Promise((resolve, _) => {
setTimeout(() => {
resolve(1);
}, 100);
});

const promiseGenerator2: () => Promise = () =>
new Promise((resolve, _) => {
setTimeout(() => {
resolve(2);
}, 300);
});

const promiseGenerator3: () => Promise = () =>
new Promise((resolve, _) => {
setTimeout(() => {
resolve(3);
}, 200);
});

const batchSize = 2;
const results = await makePromiseBatch(
[promiseGenerator1, promiseGenerator2, promiseGenerator3],
batchSize
);

console.log(results); // [ [ 1, 2 ], [ 3 ] ]
```