https://github.com/snatalenko/async-parallel-pipe
https://github.com/snatalenko/async-parallel-pipe
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/snatalenko/async-parallel-pipe
- Owner: snatalenko
- Created: 2025-03-13T16:11:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-13T16:26:39.000Z (over 1 year ago)
- Last Synced: 2025-03-13T17:37:01.841Z (over 1 year ago)
- Language: JavaScript
- Size: 71.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
Async Parallel Pipe
===================
[](https://www.npmjs.com/package/async-parallel-pipe)
[](https://github.com/snatalenko/async-parallel-pipe/actions/workflows/audit.yml)
[](https://github.com/snatalenko/async-parallel-pipe/actions/workflows/tests.yml)
[](https://coveralls.io/github/snatalenko/async-parallel-pipe?branch=main)
[](https://www.npmjs.com/package/async-parallel-pipe)
The function pulls input values from async iterable input, runs async action, and returns async iterable output in a same order as input was received.
Here is an example that requests random numbers from random.org with not more than 2 concurrent requests at a time:
```js
const { parallelPipe } = require('async-parallel-pipe');
const fetch = require('node-fetch');
async function getRandomNumber(maxValue) {
const response = await fetch(`https://www.random.org/integers/?num=1&min=0&max=${maxValue}&col=1&base=10&format=plain&rnd=new`);
const value = await response.text();
return value;
}
(async function main() {
const inputIterable = [1, 2, 3, 4, 5].values();
const concurrentThreads = 2;
const outputIterable = parallelPipe(inputIterable, concurrentThreads, getRandomNumber);
for await (const value of outputIterable)
console.log(value);
}());
```
Since input and output are generators, multiple parallel executions can be chained together:
```js
const inputIterable = [1, 2, 3, 4, 5].values();
const multipliedIterable = parallelPipe(inputIterable, 3, el => el * 100);
const sumIterable = parallelPipe(multipliedIterable, 3, el => el + 2);
for await (const value of sumIterable)
console.log(value);
```