Skip to content

Do I need to unsubscribe from an observable that completed or errored? #7195

Answered by demensky
crumbyshirt asked this question in Q&A
Discussion options

You must be logged in to vote

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);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@crumbyshirt
Comment options

Answer selected by crumbyshirt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants