mergeiterator
Merges async iterators.
Merges list of async or sync iterables into async one.
It accepts any iterable like arrays, generators, async generators or even promises which resolve to iterables.
Any iterator can be infinite, including the list of iterables itself.
import merge from "mergeiterator"
async function DoIt() {
for await (const v of merge([
[1, 2, Promise.resolve(3)],
Promise.resolve([4, 5]),
(function*() {
let i = 9
while (true) {
yield i++
yield Promise.resolve(i++)
}
})(),
(async function*() {
yield 6
yield await Promise.resolve(7)
yield Promise.resolve(8)
})(),
])) {
console.log(v)
}
}
This function guarantees, that if some value is yielded by some of iterables, then that value will be eventually yielded. This is basically about infinite iterables.
It also guarantees that the order of values within the same iterable is preserved.
If some iterable yields a promise, its value will be used, not a promise itself.
If some iterable throws an error, that error will be redirected to a caller and other iterables will be closed.
The return value of merge
is the return value of the list of iterables. Return values of merged iterables are discarded.
API
Table of Contents
merge
Merges async or sync iterables into async one.
Parameters
sequences
AnyIterable<AnyIterable<T>, ReturnT>
Returns AsyncGenerator<T, ReturnT, void>