aggregate-async-iterator
Version:
Aggregates several async iterators into one (zip)
117 lines (86 loc) • 3.63 kB
Markdown
[](https://www.npmjs.com/package/aggregate-async-iterator)
[](https://spdx.org/licenses/0BSD.html)
[](https://typescriptlang.org)
[](https://bundlejs.com/?q=aggregate-async-iterator)
[](https://npmjs.org/package/aggregate-async-iterator)
[](https://github.com/arlac77/aggregate-async-iterator/issues)
[](https://actions-badge.atrox.dev/arlac77/aggregate-async-iterator/goto)
[](https://github.com/prettier/prettier)
[](http://commitizen.github.io/cz-cli/)
[](https://snyk.io/test/github/arlac77/aggregate-async-iterator)
[](https://coveralls.io/github/arlac77/aggregate-async-iterator)
Aggregates several async iterators into one (zip)
# usage
```js
import { aggregateRoundRobin, aggregateFifo } from "aggregate-async-iterator";
async function* sequence(name, time = 100, num = 10) {
for (let i = 0; i < num; i += 1) {
yield new Promise(resolve => setTimeout(resolve(name + i), time));
}
}
console.log("RR:");
for await (const r of aggregateRoundRobin([
sequence("A", 100, 3),
sequence("B", 35, 5)
])) {
console.log(r);
}
console.log("FIFO:");
for await (const r of aggregateFifo([
sequence("A", 100, 3),
sequence("B", 35, 5)
])) {
console.log(r);
}
```
Prints interleaved sequences
```txt
RR:
A0
B0
A1
B1
A2
B2
B3
B4
FIFO:
A0
B0
A1
B1
A2
B2
B3
B4
```
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
* [aggregateFifo](
* [Parameters](
* [aggregateRoundRobin](
* [Parameters](
Aggregate items from sevaral async iterators into one.
Items are collected first in first out from the sources.
Whatever source comes first will be delivered first.
* `sources` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<AsyncIterator\<any>>** 
Returns **AsyncIterable\<any>** items collected from all sources
Aggregate items from sevaral async iterators into one.
Items are collected round robin from the sources.
The 2nd. round of items will only be delivered after all sources
have delivered their 1st. round (or reached their end).
* `sources` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<AsyncIterator\<any>>** 
Returns **AsyncIterable\<any>** items collected from all sources
With [npm](http://npmjs.org) do:
```shell
npm install aggregate-async-iterator
```
BSD-2-Clause