Combine-async-iterators
Combine Multiple Asynchronous Iterators in one (not a sequence). It use Promise.race under the hood (the code idea is from Targos).
Requirements
Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i combine-async-iterators
$ yarn add combine-async-iterators
Usage example
const { promisify } = require("util");
const combineAsyncIterators = require("combine-async-iterators");
const sleep = promisify(setTimeout);
async function* getValues(id) {
for (let count = 0; count < 5; count++) {
await sleep(Math.ceil(Math.random() * 1000));
yield `${id}_${count}`;
}
}
async function main() {
const asyncIterator = combineAsyncIterators(getValues("first"), getValues("second"));
for await (const value of asyncIterator) {
console.log(value);
}
}
main().catch(console.error);
API
function combineAsyncIterators(...iterators: AsyncIterableIterator<any>[]): AsyncIterableIterator<any>
Licence
MIT