Angular RxJS: Why I was wrong, You should always use subscribes, not promises.

Joshua Markasky
3 min readNov 27, 2020

I wrote a post before saying why you should use promises and only use subscribes. Oh how wrong I was, the thing about angular and its newest reincarnation’s is that it incorporates many different libraries and technologies. One of them being RxJS, and RxJs isn’t your typical library. It revolves around observables, observers, and Subjects. Area’s where I feel a lot of web development hasn’t really go into in depth. Even after reading the documentation of RxJs, it can be really confusing sooo I’m going to show example why Subscribes should always be used and try to simplify observables and other concepts of RxJs.

Lets start with the main advantages of Subscribes. Lets look at a very basic Angular Material Table with pagination.

Every time we hit the next page button and that made a call to pull data from a Database to show the results for the next page. If we used promise’s and a user continuously clicked the next page button, we would send multiple calls that would have to be resolves. Overloading the sever with many calls that we do not need to be resolved. So what’s the solution? Well we could programing it to where if a user hits the next page button, it wouldn’t allow the user to click the button again. Problem solve right? Well while that might stop a user from making a bunch of a unnecessary calls, it completely lowers the user experience. If the user needs to get to page 44, and has to stop and wait for the call to be resolve before being able to make a other one. you can see how going page by page would be a real pain.

So What happens if we use subscribes? Well in my last post I said the main advantage of subscribes is that it is cancelable!!!! So yes!!! if we turn the promise into a subscription and cancelled the call if it gets called again and still not resolve will allow use to keep great user experience as well as not overloading our rest API with unnecessary calls, improving performance.

Turn this

To this

Now every time a new call is made and the old one isn’t resolved we will see cancelled calls

I plan on going deeper into RxJs and more into the different ways to use subscribes!!!! I’ll see you guys in the next one

--

--