From 490d2a5ca1acff12ca0f47db8d654848046e3149 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 16 Jun 2020 02:03:07 +1000 Subject: fix: MuxAsyncIterator throws muxed errors (#6295) Fixes #5260 --- std/async/mux_async_iterator.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'std/async/mux_async_iterator.ts') diff --git a/std/async/mux_async_iterator.ts b/std/async/mux_async_iterator.ts index b32689a29..ba6f22775 100644 --- a/std/async/mux_async_iterator.ts +++ b/std/async/mux_async_iterator.ts @@ -7,14 +7,15 @@ interface TaggedYieldedValue { } /** The MuxAsyncIterator class multiplexes multiple async iterators into a - * single stream. It currently makes a few assumptions: - * - The iterators do not throw. + * single stream. It currently makes an assumption: * - The final result (the value returned and not yielded from the iterator) * does not matter; if there is any, it is discarded. */ export class MuxAsyncIterator implements AsyncIterable { private iteratorCount = 0; private yields: Array> = []; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private throws: any[] = []; private signal: Deferred = deferred(); add(iterator: AsyncIterableIterator): void { @@ -25,11 +26,15 @@ export class MuxAsyncIterator implements AsyncIterable { private async callIteratorNext( iterator: AsyncIterableIterator ): Promise { - const { value, done } = await iterator.next(); - if (done) { - --this.iteratorCount; - } else { - this.yields.push({ iterator, value }); + try { + const { value, done } = await iterator.next(); + if (done) { + --this.iteratorCount; + } else { + this.yields.push({ iterator, value }); + } + } catch (e) { + this.throws.push(e); } this.signal.resolve(); } @@ -46,6 +51,12 @@ export class MuxAsyncIterator implements AsyncIterable { this.callIteratorNext(iterator); } + if (this.throws.length) { + for (const e of this.throws) { + throw e; + } + this.throws.length = 0; + } // Clear the `yields` list and reset the `signal` promise. this.yields.length = 0; this.signal = deferred(); -- cgit v1.2.3