In the previous post about RxJS, I set myself a target of answering a few questions. This post in particular deals with the following:

What other advantages does declarative style offer?

Let’s answer this question in the context of RxJS in particular.

1. Streams are cheap

An observable is nothing but a stream. A stream is nothing but an array. To be specific, both in nature are collections. The only difference is that in a stream the data items are emitted over time. And arrays are considered to be cheap enough. So, all that is left is the wrapping it with some listener magic. Since a simple event handler isn’t expensive either, the listening act of observables too will be inexpensive.

Play with the code here.

2. Memory leak prevention

Leaving the playground cleaner than you found it is a nice rule of thumb. With Observables, you get to `complete` it. Completing an `observable` expires all its subscriptions and cleans itself up. And offers a readymade way of preventing memory leaks. And you can concentrate on getting work done.

However, one can argue that with the modern browsers, unresolved promises is not a problem anymore. Find more about this here. But still, when dealing with complex chaining of promises, clean up does get tricky. It is a bit easier with observables, but, one still needs to use their `complete`s well.

Play with the code here.

3. A better alternative to promises?

Observables offer everything promises do. If I write an observable next to a promise, I don’t see a lot of difference.

Play with the code here.

However, an observable can be completed, unlike promise. But, what advantage does that offer? It offers something that isn’t very easy to achieve with promises. i.e. canceling. Yes, with observables, canceling is as simple as completing the observable and shutting down the subscribers. A similar teardown is simply not possible with promises.

Cancelling is as simple as `unsubscribe` with RxJS. This hacky stuff is a way to introduce cancellation to promises. But really is a hack.

Another subtle difference is that promises are built for a single response. But, that really isn’t a huge difference for me.

4. Functional

The whole package includes the goodies of functional programming. And above all, you don’t really need to play catch with functional programming while diving into reactive programming.

5. The server side

Since I am writing this blog to reach out to the frontend developers out there, it’s my duty to mention that the reactive programming is already very popular with backend technologies as well. And getting used to reactivity will also help you with your server adventures.

Read more

6. Better tools

RxJS being a full-fledged library offers some great tools like retry, replay e.t.c. I’ll leave this to you to find out more about these.

Conclusion

RxJS sounds awesome. One doesn’t miss out on the advantages offered by promises, functional programming and what not. So, that is a plus. Memory management isn’t a problem. However, it’ll be interesting to know how exception handling and subscription handling turns out to be with RxJS.

Please let me know if I have missed any other positives of using RxJS.