[RxJS] Reactive Programming - Why choose RxJS?

RxJS is super when dealing with the dynamic value. 

 

Let's see an example which not using RxJS:

var a = 4;
var b = a * 10;

console.log(b); // 40

a = 5;
console.log(b); // 40

So you change a, it won't affect b's value because b is already defined....

So if you want b get sideeffect, you need to declear it again:

var a = 4;
var b = a * 10;

console.log(b); // 40

a = 5;
b = a * 12;
console.log(b); // 60

 And also, many a time, it may happened that you found the variable has been mutated, but you have no idea where and how.

 

So using RxJS:

var streamA = Rx.Observable.interval(400).take(5);
var streamB = streamA.map( (num) => {
  return num * 10;
})

streamB.subscribe(b => console.log(b));

SO now, streamA arrivals at every 400ms, we can take 4 of those. then we map to StreamB.

Then in the console, you will see the value will change according to the value of stream A.

你可能感兴趣的:([RxJS] Reactive Programming - Why choose RxJS?)