[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(, );
const width$ = Rx.Observable.of(,);
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();
const width$ = Rx.Observable.of();
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();
const width$ = Rx.Observable.of();
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().take() 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())
[RxJS] Replace zip with combineLatest when combining sources of data的更多相关文章
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
- python3 解决zip解压中文乱码问题,亲测可用, ZipFile
中文乱码是个很头疼的问题,找了好久都没用找到解决办法 后来也忘了在哪儿找到的解决办法, 很久以前了,但不可行, 解决了思路 在源码里面想要修改内容 if flags & 0x800: # UT ...
- rxjs入门6之合并数据流
一 concat,merge,zip,combineLatest等合并类操作符 以上操作符在版本6中已经只存在静态方法,不能在pipe中使用. import {concat,merge,zip,com ...
- 【转】Rxjs知识整理
原文:https://www.jianshu.com/p/16be96d69143 ---------------------------------------------------------- ...
- RxJS 6有哪些新变化?
我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6. rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...
- java zip 工具类
原文:http://www.open-open.com/code/view/1430906539866 package com.topsoft.websites.utils; import java. ...
- Java 解压 zip 文件
代码如下 package test_java; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcep ...
- Like ruby of SBM Crusher zip to dict
how to use the zip to bulid a dict in python? data = """A dynamic, interpreted, open ...
随机推荐
- AtomicInteger类
今天写代码.尝试使用了AtomicInteger这个类,感觉使用起来非常爽,特别适用于高并发訪问.能保证i++,++id等系列操作的原子性. ++i和i++操作并非线程安全的.非常多人会用到synch ...
- 细说 iOS 消息推送
APNS的推送机制 与Android上我们自己实现的推送服务不一样,Apple对设备的控制很严格.消息推送的流程必需要经过APNs: 这里 Provider 是指某个应用的Developer,当然假设 ...
- ubuntu网络重启后或主机重启后,/etc/resolv.conf恢复原样的解决办法
ubuntu网络重启后或主机重启后,/etc/resolv.conf恢复原样的解决办法 /etc/resolv.conf中设置dns之后每次重启该文件会被覆盖,针对这种情况找了一些个解决方法 防止/e ...
- theme-不同主题资源更改
1.找到了影响桌面小部件的布局文件packages/apps/Mms$ vim res/layout/widget.xml修改里面的背景颜色属性,可以实现预期效果,至于里面的 <LinearLa ...
- alert警告框
标签中写: <div class="alert alert-warning fade in"> <button class="close" d ...
- windows新建或者重命名文件及文件夹必须手动刷新才能显示出来
平台:win8.1 问题:windows新建或者重命名文件及文件夹必须手动刷新才能显示出来 解决方法: 注册表中HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ ...
- 使用dotcloud免费ssh
使用dotcloud免费ssh https://www.dotcloud.com一个项目在线托管网站,注册后可以免费托管两个项目. 注册帐号,在ubuntu中执行下面命令,安装dotcloud环境 s ...
- import 与export详解
ES6 1.export default 其他模块加载该模块时,import命令可以为该匿名函数指定任意名字. 如: import Vue from 'vue' vue里面的第三方模块都是用了这个 使 ...
- HDOJ 5409 CRB and Graph 无向图缩块
无向图缩块后,以n所在的块为根节点,dp找每块中的最大值. 对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1 CRB and Graph Time Limit: 8000/4000 MS ( ...
- synchronized和AtomicInteger解决并发问题的性能比较
AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而volatile ...