zoukankan      html  css  js  c++  java
  • [RxJS] Reactive Programming

    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.

  • 相关阅读:
    python 时间等待
    python threading多线程
    c 字符串的结束标志
    c 输出是自动显示输出类型
    c 的占位符
    c数据类型
    游戏引擎
    java 数据类型
    python 读写json数据
    python 多线程_thread
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5252572.html
Copyright © 2011-2022 走看看