Avoid multiple subscriptions while working with Observables

Don’t subscribe to often while working with RxJs. If you are in a situation where you need to get values from many observables in your component don’t manage it imperatively by subscribing to all of them separately but take a full advantage of RxJs and compose your observables.

To get an impression how this approach might help you take a look on following code.

I want to render in my component a filtered and sorted list of cards. All data is stored in ngrx/store and therefore I need to select it at first. By default store.select method returns an observable so I finally end up having two observables with data i need to perform the task. By using the combineLatest method of Observable I get one observable which passes an array of latest values from source observables to its subscriber. This enables me to work with my data from store very conveniently.



Founder, alphta.consulting

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store