Avoid multiple subscriptions while working with Observables

Łukasz Gawryś
Oct 26, 2017 · 1 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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