How to unsubscribe from RxJS 5?

I used this template:

func myObservable() Observable<boolean> { ... } func myFunc() { myObservable().subscribe((cond: boolean) => { if (cond) { // How do I unsubscribe here? } }); } 

However, I see no way to unsubscribe, which could lead to a memory leak.

The reason I'm asking is because the Angular 2 HTTP client uses the same template - although I believe that it automatically cancels the subscription and I would like to do the same.

+7
javascript angularjs angular typescript rxjs5
source share
1 answer

You should do something like this:

 func myFunc() { var subscription = myObservable().subscribe((cond: boolean) => { if (cond) { // How do I unsubscribe here? subscription.unsubscribe() } }); } 
+11
source share

All Articles