Skip to content

Commit

Permalink
fix(src/asynciterable/operators/batch.ts): create rejected Promise on…
Browse files Browse the repository at this point in the history
… demand (#328)

fix #320
  • Loading branch information
trxcllnt authored Jun 9, 2021
1 parent 084658f commit aa40ab1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/asynciterable/operators/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class BatchAsyncIterable<TSource> extends AsyncIterableX<TSource[]> {

let state: State<TSource> = { type: BATCHING_TYPE, values: [] };
let ended: null | Promise<IteratorResult<TSource[]>> = null;
let error: any = null;

function consumeNext() {
it.next().then(
Expand All @@ -65,11 +66,9 @@ export class BatchAsyncIterable<TSource> extends AsyncIterableX<TSource[]> {
}
},
(err) => {
ended = Promise.reject(err);

error = err;
if (state.type === WAITING_TYPE) {
const { reject } = state.resolver;
reject(err);
state.resolver.reject(err);
}
}
);
Expand All @@ -79,6 +78,10 @@ export class BatchAsyncIterable<TSource> extends AsyncIterableX<TSource[]> {

return {
next() {
if (error) {
return Promise.reject(error);
}

if (state.type === BATCHING_TYPE && state.values.length > 0) {
const { values } = state;
state.values = [];
Expand Down

0 comments on commit aa40ab1

Please sign in to comment.