I am currently working on a web application which uses angular 6 as its frontend framework. I am also going to use some api endpoints to get data from server. And here i got stuck, according to angular docs it recommends to use rxjs but i am little bit confused while using rxjs operators.
-
The HttpClient can do more than just make a simple request, in can stream progress events etc. Those are things that cannot be done using promises. Also promises cannot be canceled, but observables can, allowing you to cancel requests. You can still convert an observable to a Promise and use async / await if you prefer to do so and don't want to go into reactive programming. – Ingo Bürk Sep 16 '18 at 06:39
2 Answers
Probably if you consider only http calls to APIs, the advantages brought by RxJS vs Promises are not that many. The retry
operator makes it easier to retry when errors occur, maybe managing race conditions is easier using switchMap
, but overall not that much.
The reason is that http calls are a "one shot" thing. You fire 1 call and that 1 call returns just 1 result or errors. Just like Promeses are "one shot" things.
Where RxJS really shines is when you have to deal with streams of events that may have more than 1 emission over time. The Cloud and the DOM are 2 examples of sources of such streams. These are the situations where you can see how RxJS is Promises on steroids.
Here a couple of examples:

- 16,775
- 13
- 70
- 113
Observable is
cancelable
if we unsubscribe from http call before it's done - http call will be aborted.Observable has better composability, for example: by default Promise have 1 strategy for flattening:
promise1.then(()=>promise2)
, RxJs have many:Observable more expressive it is like Lodash for async action (have many helpful operators)

- 3,539
- 1
- 21
- 38