diff --git a/src/asynciterable/operators/batch.ts b/src/asynciterable/operators/batch.ts index 0defc06d..4126998a 100644 --- a/src/asynciterable/operators/batch.ts +++ b/src/asynciterable/operators/batch.ts @@ -40,6 +40,7 @@ export class BatchAsyncIterable extends AsyncIterableX { let state: State = { type: BATCHING_TYPE, values: [] }; let ended: null | Promise> = null; + let error: any = null; function consumeNext() { it.next().then( @@ -65,11 +66,9 @@ export class BatchAsyncIterable extends AsyncIterableX { } }, (err) => { - ended = Promise.reject(err); - + error = err; if (state.type === WAITING_TYPE) { - const { reject } = state.resolver; - reject(err); + state.resolver.reject(err); } } ); @@ -79,6 +78,10 @@ export class BatchAsyncIterable extends AsyncIterableX { return { next() { + if (error) { + return Promise.reject(error); + } + if (state.type === BATCHING_TYPE && state.values.length > 0) { const { values } = state; state.values = [];