mergeSortedAsync<
A>(order,sources):AsyncGenerator<Awaited<A>,void,unknown>
Defined in: packages/sorted-merge/src/merge-sorted-async.ts:60
Lazily merge a finite collection of independently iterable sorted sources, async or sync, under the same total order. Keeps duplicates. Equal items retain their order within each source, with earlier sources winning ties. Supply a stable order and keep compared values unchanged while the merge is active.
Each source is opened through Symbol.asyncIterator when it has one and through Symbol.iterator otherwise, so
async streams and in-memory arrays mix freely. Items are compared as the sources produce them; the merge does not
await an item that is itself a promise before comparing it. The generator yields its resolved value. A rejected
item rejects the advance and triggers source cleanup.
The first advance opens every source in collection order, then awaits each source’s first next() concurrently and waits for all of them to settle. If any of them rejects, no read is still in flight when the merge closes the opened sources, and the rejection from the earliest source in collection order propagates. Each subsequent advance awaits next() only on the source of the last emitted value. With k sources, setup takes O(k) time and space, and each output after setup takes O(log(k + 1)) time, excluding input reads and comparator cost. At most one item per source is buffered. Individual sources may be infinite.
On early return or failure, awaits return() once on each opened, unfinished iterator that exposes it, one at a time in collection order. Errors from the order and input iterators propagate. A cleanup error surfaces if the merge was otherwise returning normally. When several iterators fail during cleanup, the first error surfaces after all are tried.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Order<A>
sources
Section titled “sources”Iterable<AsyncIterable<A, any, any> | Iterable<A, any, any>>
Returns
Section titled “Returns”AsyncGenerator<Awaited<A>, void, unknown>