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的更多相关文章

  1. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  2. Angular快速学习笔记(4) -- Observable与RxJS

    介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...

  3. python3 解决zip解压中文乱码问题,亲测可用, ZipFile

    中文乱码是个很头疼的问题,找了好久都没用找到解决办法 后来也忘了在哪儿找到的解决办法, 很久以前了,但不可行, 解决了思路 在源码里面想要修改内容 if flags & 0x800: # UT ...

  4. rxjs入门6之合并数据流

    一 concat,merge,zip,combineLatest等合并类操作符 以上操作符在版本6中已经只存在静态方法,不能在pipe中使用. import {concat,merge,zip,com ...

  5. 【转】Rxjs知识整理

    原文:https://www.jianshu.com/p/16be96d69143 ---------------------------------------------------------- ...

  6. RxJS 6有哪些新变化?

    我们的前端工程由Angular4升级到Angular6,rxjs也要升级到rxjs6.  rxjs6的语法做了很大的改动,幸亏引入了rxjs-compact包,否则升级工作会无法按时完成. 按照官方的 ...

  7. java zip 工具类

    原文:http://www.open-open.com/code/view/1430906539866 package com.topsoft.websites.utils; import java. ...

  8. Java 解压 zip 文件

    代码如下 package test_java; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcep ...

  9. Like ruby of SBM Crusher zip to dict

    how to use the zip to bulid a dict in python? data = """A dynamic, interpreted, open ...

随机推荐

  1. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) A】 Perfect Squares

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] N*1000暴力就好 [代码] #include <bits/stdc++.h> using namespace std ...

  2. Spring有用功能--Profile、WebService、缓存、消息、ORM

    本篇介绍一些Spring与其它框架结合的有用功能,包含:Apache CXF WebService框架.Redis缓存.RabbitMQ消息.MyBatis框架. 另外对于Profile,也是Spri ...

  3. 重构insert update 比较两个datatbale

    #region 下载时重构insert(数据带null处理) public void DownDataInsert(DataTable _dt, string TableName,DBHelper d ...

  4. 【试水CAS-4.0.3】第07节_CASclient配置单点登录

    完整版见https://jadyer.github.io/2015/07/26/sso-cas-client-login/ 本文源代码下载:http://download.csdn.net/detai ...

  5. Nginx系列(一)--nginx是什么?

    一.介绍 Nginx是一个高性能的HTTP和反向代理server,也是一个IMAP/POP3/SMTP代理server. Nginx是一款轻量级的Webserver/反向代理server以及电子邮件代 ...

  6. (43)JS运动之链式运动框架

    链式运动框架就是一系列的运动分阶段进行,在普通的运动框架上加上一个參数function,这个function表示下一个要运行的动作.详细代码例如以下: <!DOCTYPE HTML> &l ...

  7. try {}里有一个return语句 finally执行顺序

    先看例子 package example; class Demo{ public static void main(String args[]) { int x=1; System.out.print ...

  8. CF-558E (线段树/分块)

    解题思路: 很显然突破口就是字符集比较小,分块和线段树都能A 话说线段树时间是分块5倍啊 代码(线段树): #include<cstdio> #include<cstring> ...

  9. 洛谷 P2504 [HAOI2006]聪明的猴子

    洛谷 P2504 [HAOI2006]聪明的猴子 题目描述 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水 ...

  10. 《机器学习实战》基于朴素贝叶斯分类算法构建文本分类器的Python实现

    ============================================================================================ <机器学 ...