Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forEach): properly unsubs after error in next handler #6677

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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