码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Replace zip with combineLatest when combining sources of data

时间:2017-05-29 15:22:38      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ide   ble   this   ssi   pre   each   div   can   cti   

This lesson will highlight the true purpose of the zip operator, and how uncommon its use cases are. In its place, we will learn how to use the combineLatest operator.

const length$ = Rx.Observable.of(5, 4);
const width$ = Rx.Observable.of(7,1);
const height$ = Rx.Observable.of(2.8, 2.5);

const volume$ = Rx.Observable
  .zip(length$, width$, height$,
    (length, width, height) => length * width * height
  );

volume$.subscribe(function (volume) {
  console.log(volume); 
});

zip requiers each observable has synchronized emissions.  It means:

const length$ = Rx.Observable.of(5);
const width$ = Rx.Observable.of(7);
const height$ = Rx.Observable.of(2.8, 2.5);

2.5 won‘t be calculated only when lenth$ and width$ provide second value.

 

In this case we can use combineLatest instead of zip:

const length$ = Rx.Observable.of(5);
const width$ = Rx.Observable.of(7);
const height$ = Rx.Observable.of(2.8, 2.5);

const volume$ = Rx.Observable
  .combineLatest(length$, width$, height$,
    (length, width, height) => length * width * height
  );

volume$.subscribe(function (volume) {
  console.log(volume); 
});

 

[RxJS] Replace zip with combineLatest when combining sources of data

标签:ide   ble   this   ssi   pre   each   div   can   cti   

原文地址:http://www.cnblogs.com/Answer1215/p/6918001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!