Rxjs observables to replace promises?


#1

Hi there, here’s the thing, i make heavy use of promises to wrap both SqLite and http calls (yes i know http calls already return observables, just not enough experience with them to use them instead of promises, so it’s a promise wrapping a http observable), i want to start using the observables, is it worth the big refactor? if so, do i need to call unsubscribe on the ngOnDestroy() method on each page?

Also my server API doesn’t make use of sockets, most of the features are based on user interaction like button clicks, any toughs? maybe i have to make an observable for the buttons that then merges to an http call and then i could use the operators.


#2

Yes, from discussing on the angular gitter.im, they have been saying that Observables does everything Promises can and plus more features. And ideally you should be using one consistent method to handle async code, and not mix the two.

Plus, Observables have a .toPromise() method which changes it into a promise anyway.

To answer your unsubscribe method, the answer is… it depends. Most of the time, Observables are finite, as in, they return something and it gets automatically unsubscribed. Http is a good example. However, there are infinite Observables where they just keep pumping out data, and those ones you DO need to unsubscribe.

Another compelling reason to use Observables is the async pipe in Angular. This makes it very easy to put data in the view. Using promises will look more verbose and ugly.

Observables > async + await > Promises > callback.

Always use Observables