What’s Reactive?

Let’s start by looking at a little reactive RxJS program. This program needs to retrieve data from different sources with the click of a button, and it has the following requirements:

  • It must unify data from two different locations that use different JSON structures.

  • The final result should not contain any duplicates.

  • To avoid requesting data too many times, the user should not be able to click the button more than once per second.

Using RxJS, we would write something like this:

 var​ button = document.getElementById(​'retrieveDataBtn'​);
 var​ source1 = Rx.DOM.getJSON(​'/resource1'​).pluck(​'name'​);
 var​ source2 = Rx.DOM.getJSON(​'/resource2'​).pluck(​'props'​, ​'name'​);
 
 function​ getResults(amount) {
 

Get Reactive Programming with RxJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.