asyncPool
Tags: javascript, node, bug
Published: 2021-10-06 14:00:38

##Goal
Run multiple promise-returning & async functions with limited concurrency using native ES6/ES7

## Why?

Existing solutions also re-implement Promise 😩...

The goal of this library is to use native async functions (if ES7 is available) and/or native Promise (ES6) including `Promise.race()` and `Promise.all()` to implement the concurrency behavior (look our source code).

## What?

`asyncPool` runs multiple promise-returning & async functions in a limited concurrency pool. It rejects immediately as soon as one of the promises rejects. It resolves when all the promises completes. It calls the iterator function as soon as possible (under concurrency limit). For example:

```js
const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
await asyncPool(2, [1000, 5000, 3000, 2000], timeout);
// Call iterator (i = 1000)
// Call iterator (i = 5000)
// Pool limit of 2 reached, wait for the quicker one to complete...
// 1000 finishes
// Call iterator (i = 3000)
// Pool limit of 2 reached, wait for the quicker one to complete...
// 3000 finishes
// Call iterator (i = 2000)
// Itaration is complete, wait until running ones complete...
// 5000 finishes
// 2000 finishes
// Resolves, results are passed in given array order `[1000, 5000, 3000, 2000]`.
```

## Usage

```
$ npm install tiny-async-pool
```

```js
import asyncPool from "tiny-async-pool";
```

### ES7 async

```js
const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
const results = await asyncPool(2, [1000, 5000, 3000, 2000], timeout);
```

Note: Something really nice will be possible soon https://github.com/tc39/proposal-async-iteration

### ES6 Promise

```js
const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
return asyncPool(2, [1000, 5000, 3000, 2000], timeout).then(results => {
  ...
});
```

## API

### `asyncPool(poolLimit, array, iteratorFn)`

Runs multiple promise-returning & async functions in a limited concurrency pool. It rejects immediately as soon as one of the promises rejects. It resolves when all the promises completes. It calls the iterator function as soon as possible (under concurrency limit).

#### poolLimit

The pool limit number (>= 1).

#### array

Input array.

#### iteratorFn

Iterator function that takes two arguments (array item and the array itself). The iterator function should either return a promise or be an async function.

##Soon on My Git
<br/>
<h3 align="left">Connect with me:</h3>
<p box-align= "baseline">
<a href="https://discord.gg/HKxUV6jXPj">
  <img  alt="AmnaAbd's Discord" align="left" width="22px"  display="initial" margin="0" src="https://raw.githubusercontent.com/peterthehan/peterthehan/master/assets/discord.svg" />
</a>
<a href="https://twitter.com/AmnaAbd22">
  <img  alt="Amna Abdallah | Twitter" align="left" width="22px"  display="initial" margin="0" src="https://raw.githubusercontent.com/peterthehan/peterthehan/master/assets/twitter.svg" />
</a>
<a href="https://www.linkedin.com/in/amna-abdallah-2b615572/">
  <img  alt="AmnaAbd's LinkedIN" align="left" width="22px" display="initial" margin="0" src="https://raw.githubusercontent.com/peterthehan/peterthehan/master/assets/linkedin.svg" />
</a>
<a href="https://open.spotify.com/user/vzhpqhqmrep0yz70cxzpi45b5">
  <img align="left" alt="Amna's Spotify" align="left" width="22px" src="https://raw.githubusercontent.com/peterthehan/peterthehan/master/assets/spotify.svg" />
</a>
<a href="https://codepen.io/AmnaAbd" target="blank"><img src="https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/codepen.svg" alt="amnaabd" align="left" height="30" width="40" display="initial" margin="0"/></a>
<a href="https://dev.to/amnaabd" target="blank"><img  src="https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/dev-dot-to.svg" alt="amnaabd" align="left" height="30" width="40" display="initial" margin="0"/></a>
<a href="https://instagram.com/amna.amouna.3114" target="blank"><img src="https://cdn.jsdelivr.net/npm/simple-icons@3.0.1/icons/instagram.svg" alt="amnaabd" align="left" height="30" width="40" display="initial" margin="0"/></a>
</p>