Do I need to unsubscribe from an observable that completed or errored? #7195
Answered
by
demensky
crumbyshirt
asked this question in
Q&A
-
I understand that it is good practice to unsubscribe from an observable whenever the subscription is no longer needed. What I want to know is will my subscription implicitly unsubscribe from the observable it was subscribed to when that observable completes? |
Beta Was this translation helpful? Give feedback.
Answered by
demensky
Feb 25, 2023
Replies: 1 comment 1 reply
-
No need. const o = new Observable((subscriber) => {
setTimeout(() => {
subscriber.complete(); // log 'complete' & log 'teardown'
}, 1000);
return () => {
console.log('teardown');
};
});
const s = o.subscribe({
complete: () => {
console.log('complete');
},
});
setTimeout(() => {
s.unsubscribe(); // log nothing
}, 2000); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
crumbyshirt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No need.