首页 > 代码库 > [RxJS] Replace zip with combineLatest when combining sources of data
[RxJS] Replace zip with combineLatest when combining sources of data
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。