Skip to content

Commit

Permalink
fix(forEach): properly unsubs after error in next handler (#6677)
Browse files Browse the repository at this point in the history
fixes #6676
  • Loading branch information
benlesh committed Dec 6, 2021
1 parent 8cb201c commit b9ab67d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
6 changes: 3 additions & 3 deletions spec/Observable-spec.ts
Expand Up @@ -153,9 +153,9 @@ describe('Observable', () => {
},
(err) => {
results.push(err);
// Since the consuming code can no longer interfere with the synchronous
// producer, the remaining results are nexted.
expect(results).to.deep.equal([1, 2, 3, 4, expected]);
// The error should unsubscribe from the source, meaning we
// should not see the number 4.
expect(results).to.deep.equal([1, 2, 3, expected]);
}
);
});
Expand Down
16 changes: 7 additions & 9 deletions src/internal/Observable.ts
Expand Up @@ -313,21 +313,19 @@ export class Observable<T> implements Subscribable<T> {
promiseCtor = getPromiseCtor(promiseCtor);

return new promiseCtor<void>((resolve, reject) => {
// Must be declared in a separate statement to avoid a ReferenceError when
// accessing subscription below in the closure due to Temporal Dead Zone.
let subscription: Subscription;
subscription = this.subscribe(
(value) => {
const subscriber = new SafeSubscriber<T>({
next: (value) => {
try {
next(value);
} catch (err) {
reject(err);
subscription?.unsubscribe();
subscriber.unsubscribe();
}
},
reject,
resolve
);
error: reject,
complete: resolve,
});
this.subscribe(subscriber);
}) as Promise<void>;
}

Expand Down

0 comments on commit b9ab67d

Please sign in to comment.