In our app we have quite a few places where we use:
someObservable.take(1).subscribe(onSuccessHandler, onFailureHandler);
But with subscriptions you need to worry about unsubscribing at some point and that is not always straightforward.
I was thinking of simplifying this and rewriting this as:
someObservable.toPromise().then(onSuccessHandler).catch(onFailureHandler);
But looking at the implementation of toPromise()
(here) I don't seem to understand why it doesn't care about unsubscribing.
The comment in the code says that no cancellation could be done, but how we just leave it like this to leak memory (in case we actually are).
EDIT
I came up with an example that worries me:
Observable.timer(10, 10).toPromise().then((v) => console.log("I'm done"));
If observables I retrieve are such that they never complete, then not only my promises never complete with a value, but also I have no way to unsubscribe from such observables (e.g. time-out them and my promises), because I have no access to a subscription object. And that does leak memory!