[RxJS] Replace zip with combineLatest when combining sources of data
zip of with Data when Rxjs replace sources
2023-09-14 09:00:51 时间
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); });
One useful tip for using zip:
zip can spread sync value over time, when combine with interval
const source$ = of('hello') const interval$ = interval(100).take(5) source$.zip(interval$, (s, n) => s) .subscribe() /* source$: (hello)| interval$ ----0----1----2----3----4 zip ----h----e----l----l----o */
The same effect can also be done with concatMap + delay
const source$ = of('hello') source$.concatMap(x => of(x).delay(100))
相关文章
- linux 解压zip文件及各种问题解决
- 06_Linux基础-NGINX和浏览器、网页的关系-云服务器ssh登陆-安装NGINX-上传网页-压缩命令-xz-gzip-bzip2-zip-tar-配置N
- linux下压缩与解压缩-tar和zip_linux打包命令
- PHP打包资源为zip
- mysql 5.7.19 zip版本 windows安装步骤详解数据库
- java压缩与解压(Java.util.zip)详解编程语言
- 压缩你的Linux:使用Zip源码(zip源码linux)
- Linux下增加Zip文件的实现方法(linux给zip加文件)
- Linux下解压Zip文件的简便方式(zip怎么linux解压)
- 7-Zip v18.05 最新正式美化增强版
- Linux安装Zip文件的指南(linux安装zip文件)
- Linux压缩工具:Zip打包操作(zip打包linux)
- 浅谈zip格式处理逻辑漏洞
- 轻松学会Linux系统下打开ZIP文件的方法(linux打开zip)
- 轻松拆包:Linux实用技巧之ZIP解压教程(linux中的解压zip)
- Linux解压Zip:一步到位(linux 解压zip)
- 探索Linux查看ZIP文件内容的方法(linux查看zip内容)
- Linux将Zip文件解压至指定文件夹(linux解压zip到指定文件夹)
- Linux解压ZIP文件到指定文件夹的操作方法(linux解压zip到指定文件夹)
- 用Mysql轻松压缩和解压Zip网址(mysql zip网址)
- 轻松搭建MySQL Zip版的环境配置教程(mysql zip版配置)
- MySQL下载教程如何获取ZIP文件版本(mysql下载是zip)
- 简易教程如何下载并安装MySQL zip压缩文件(mysql下载zip的)
- MySQL下载 Zip 格式并简单使用(mysql下载zip使用)
- 文件Oracle数据库压缩双重节约(oracle两个zip)
- MySQL压缩备份学会如何使用zip命令(mysql zip 使用)
- PHP增加了对.ZIP文件的读取功能