Skip to content

Commit b9ab67d

Browse files
authoredDec 6, 2021
fix(forEach): properly unsubs after error in next handler (#6677)
fixes #6676
1 parent 8cb201c commit b9ab67d

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed
 

‎spec/Observable-spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ describe('Observable', () => {
153153
},
154154
(err) => {
155155
results.push(err);
156-
// Since the consuming code can no longer interfere with the synchronous
157-
// producer, the remaining results are nexted.
158-
expect(results).to.deep.equal([1, 2, 3, 4, expected]);
156+
// The error should unsubscribe from the source, meaning we
157+
// should not see the number 4.
158+
expect(results).to.deep.equal([1, 2, 3, expected]);
159159
}
160160
);
161161
});

‎src/internal/Observable.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,19 @@ export class Observable<T> implements Subscribable<T> {
313313
promiseCtor = getPromiseCtor(promiseCtor);
314314

315315
return new promiseCtor<void>((resolve, reject) => {
316-
// Must be declared in a separate statement to avoid a ReferenceError when
317-
// accessing subscription below in the closure due to Temporal Dead Zone.
318-
let subscription: Subscription;
319-
subscription = this.subscribe(
320-
(value) => {
316+
const subscriber = new SafeSubscriber<T>({
317+
next: (value) => {
321318
try {
322319
next(value);
323320
} catch (err) {
324321
reject(err);
325-
subscription?.unsubscribe();
322+
subscriber.unsubscribe();
326323
}
327324
},
328-
reject,
329-
resolve
330-
);
325+
error: reject,
326+
complete: resolve,
327+
});
328+
this.subscribe(subscriber);
331329
}) as Promise<void>;
332330
}
333331

0 commit comments

Comments
 (0)
Please sign in to comment.