首页 > Web开发 > 详细

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

时间:2017-05-29 15:12:08      阅读:507      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!