Skip to content

Instantly share code, notes, and snippets.

@AKST
Created November 29, 2025 09:28
Show Gist options
  • Select an option

  • Save AKST/296bacbd576fefc00e41689b914e52b1 to your computer and use it in GitHub Desktop.

Select an option

Save AKST/296bacbd576fefc00e41689b914e52b1 to your computer and use it in GitHub Desktop.
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