[RxJS] Replace zip with combineLatest when combining sources of data
阿新 • • 發佈:2017-05-29
his cnblogs ons repl com combine zed mean pla
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); constvolume$ = 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); constheight$ = 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