6

How do I unsubscribe an observable if it's converted to promise? I'm using RxJS in Angular 2

import 'rxjs/add/operator/toPromise';

return this.http.get(this.heroesUrl)
                  .toPromise()
                  .then(this.extractData)
Ankush
  • 675
  • 2
  • 13
  • 29

1 Answers1

16

If you subscribe to an Observable then it is possible to unsubscribe to it.

When you return promise(here it seems you return promise -not observable), you usually don't subscribe to it. right??
So you can't unsubscribe to it(promise).

micronyks
  • 54,797
  • 15
  • 112
  • 146
  • 5
    How about unsubscribing of a `Observable.fromPromise()` ? – realappie Mar 06 '17 at 13:56
  • 4
    @Abdel : The unsubscribe is automatic. If you check the source code of Observable.fromPromise() , the onCompleted() function is called in the observer.https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/perf/operators/frompromise.js – Maxime Leprince Mar 29 '17 at 10:28
  • @MaximeLeprince Would that mean promises can technically be cancelled as well? – realappie Mar 29 '17 at 13:44
  • @Abdel : No because when you call subscribe, the promise is triggered. Next if you unsubscribe from the observable, you could not have the result but the promise will be neverthelesse computed – Maxime Leprince Mar 29 '17 at 22:47