[RxJS] Reactive Programming - Why choose RxJS?

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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?的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()

[RxJS] Reactive Programming - Sharing network requests with shareReplay()

[RxJS] Reactive Programming - New requests from refresh clicks -- merge()

Reactive-Extensions / RxJS和ReactiveX / rxjs之间有什么区别

全面拥抱 Reactivity: RxJS, RSocket & Svelte

RxJS的基础