async-parallel-pipe
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Async Parallel Pipe

NPM Version Audit Tests Coverage Status NPM Downloads

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:

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:

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

Readme

Keywords

none

Package Sidebar

Install

npm i async-parallel-pipe

Weekly Downloads

7

Version

1.0.2

License

MIT

Unpacked Size

25.9 kB

Total Files

9

Last publish

Collaborators

  • snatalenko