Wednesday, July 25, 2018

angular6 - What is a purpose of using rxjs observables in angular 6? And what are the advantages of rxjs over async/await?




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.


Answer



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:




No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...