Created
November 29, 2025 09:28
-
-
Save AKST/296bacbd576fefc00e41689b914e52b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export async function * mergeAsyncIterators(iterables) { | |
| const iterators = iterables.map(it => it[Symbol.asyncIterator]()); | |
| const promises = new Map(iterators.map((it, i) => [i, it.next()])); | |
| while (promises.size > 0) { | |
| const next = Array.from(promises.entries(), async ([i, p]) => [i, await p]); | |
| const [index, result] = await Promise.race(next); | |
| if (result.done) { | |
| promises.delete(index); | |
| } else { | |
| yield result.value; | |
| promises.set(index, iterators[index].next()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment