[RxJS] Combination operator: combineLatest

While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. This lesson explains what AND-style combination means, and how you can join values from two or more Observables in a formula.less

 

At the begin, there is no value emit, then bar has value 0, but foo has no value still. Therefore also no value emit, until foo has the first value 0, then output the final value as 0.spa

var foo = Rx.Observable.interval(500).take(4);
var bar = Rx.Observable.interval(300).take(5);

/*
----0----1----2----(3|)     (foo)
--0--1--2--3--(4|)          (bar)
   combineLatest((x, y) => x+y)
----01--23-4--(56)-(7|)
*/

var bmi = foo.combineLatest(bar,(x,y) => x+y);

// merge: OR
// combineLatest: AND
                               
bmi.subscribe(
  function (x) { console.log('next ' + x); },
  function (err) { console.log('error ' + err); },
  function () { console.log('done'); },
);

  /*"next 0"
"next 1"
"next 2"
"next 3"
"next 4"
"next 5"
"next 6"
"next 7"
"done"*/
相關文章
相關標籤/搜索