combine-async-iterators
Advanced tools
Comparing version 1.0.0 to 1.1.0
39
index.js
@@ -21,19 +21,32 @@ "use strict"; | ||
/** | ||
* @async | ||
* @generator | ||
* @function combineAsyncIterators | ||
* @param {...AsyncIterableIterator<any>} iterators | ||
*/ | ||
async function* combineAsyncIterators(...iterators) { | ||
let open = iterators.length; | ||
const promises = iterators.map(getNextAsyncIteratorValue); | ||
try { | ||
const promises = iterators.map(getNextAsyncIteratorValue); | ||
let open = iterators.length; | ||
do { | ||
const { value, index } = await Promise.race(promises); | ||
if (value.done) { | ||
open--; | ||
promises[index] = new Promise(() => null); | ||
} | ||
else { | ||
yield value.value; | ||
promises[index] = getNextAsyncIteratorValue(iterators[index], index); | ||
} | ||
} while (open > 0); | ||
do { | ||
const { value, index } = await Promise.race(promises); | ||
if (value.done) { | ||
open--; | ||
promises[index] = new Promise(() => null); | ||
} | ||
else { | ||
yield value.value; | ||
promises[index] = getNextAsyncIteratorValue(iterators[index], index); | ||
} | ||
} while (open > 0); | ||
} | ||
catch (err) { | ||
await Promise.all(iterators.map((it) => it.return())); | ||
throw err; | ||
} | ||
} | ||
module.exports = combineAsyncIterators; |
{ | ||
"name": "combine-async-iterators", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Combine Multiple Asynchronous Iterators in one (not a sequence)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
4703
47