[RxJS] Refactoring CombineLatest to WithLatestFrom
This lesson shows why it’s preferable to using withLatestFrom instead of combineLatest in certain scenarios.
Timer will continue until you enter the number in the input field:
timer$
.do((x)=> console.log(x))
.combineLatest(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.takeWhile((data)=> data.count <= )
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
In this case, withLatestFrom() works the same way:
timer$
.do((x)=> console.log(x))
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.takeWhile((data)=> data.count <= )
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
But let's say we only want the timer log out 3 times then it should hit the complete block, logout "complete":
timer$
.do((x)=> console.log(x))
.takeWhile((data)=> data.count <= )
.withLatestFrom(
input$.do((x)=> console.log(x)),
(timer, input)=> ({count: timer.count, text: input})
)
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , )
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
then it only works with withLatestFrom() NOT combimeLatest().
The reason for that is combimeLatest require both timer$ and input$. But withLatestFrom() only need $timer.
[RxJS] Refactoring CombineLatest to WithLatestFrom的更多相关文章
- [RxJS] Refactoring Composable Streams in RxJS, switchMap()
Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstr ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- [RxJS] Combining streams in RxJS
Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- rxjs的世界
rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...
- 《深入浅出RxJS》读书笔记
rxjs的引入 // 如果以这种方式导入rxjs,那么整个库都会导入,我们一般不可能在项目中运用到rxjs的所有功能 const Rx = require('rxjs'); 解决这个问题,可以使用深链 ...
- [RxJS] Combination operator: zip
CombineLatest and withLatestFrom are both AND-style combination operators. In this lesson, we will l ...
- [RxJS] Combination operator: withLatestFrom
Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...
- RxSwift学习笔记10:startWith/merge/zip/combineLatest/withLatestFrom/switchLatest
//startWith //该方法会在 Observable 序列开始之前插入一些事件元素.即发出事件消息之前,会先发出这些预先插入的事件消息 Observable.of(1,2,3) .startW ...
随机推荐
- Struts2+Spring4+Hibernate4整合超详细教程
Struts2.Spring4.Hibernate4整合 超详细教程 Struts2.Spring4.Hibernate4整合实例-下载 项目目的: 整合使用最新版本的三大框架(即Struts2.Sp ...
- Axure自动幻灯片制作
1.打开axure7,开始. 2.拖一个占位符组件到布局上(当然也可以是矩形.图片之类的),大小270*170,作为幻灯片的第一张片子,双击写上“第一张片子”. 3.拖一个矩形,设置形状为椭圆,调整大 ...
- validate()的配置项
1.submitHandler //通过验证成功后运行的函数 代码: $("#mainForm").validate({ ...... rules:{ username:{//此处 ...
- JQUERY1.9学习笔记 之基本过滤器(二) 等于选择器
等于选择器 :eq() 描述:选择与设定下标匹配的元素.jQuery( ":eq(index)" )jQuery( ":eq(-index)" ) <!D ...
- Symfony2目录结构说明
了解框架的目录结构是框架快速入门的一个途径,一个成熟的框架,每个功能模块都被划分存放在不同的目录. Symfony2一级目录结构: ├── app //这目录下包含了,配置文件(应用的配置文件会被im ...
- 《python基础教程》笔记之 字符串
字符串格式化 字符串格式化使用字符串格式化操作符即百分号%来实现.在%的左侧放置一个字符串(格式化字符串),而在右侧则放置希望格式化的值,可以使用一个值,如一个字符串或者数字,也可以使用多个值的元组或 ...
- GET异步 请求图片步骤
- (IBAction)getImage:(id)sender { //1,准备URL NSString *str = @"http://e.hiphotos.baidu.com/image ...
- eclipse中JSP开发环境的配置
1. Java环境 自行百度配置 2. Web Server环境安装: Web Server选择流行的Apache Tomcat .到http://tomcat.apache.org/ 处下载, ...
- java构造方法的不同
分为有参数和无参数,还有THIS的使用方法,可用于传递给类,也可用于调用其它构造方法. public class Book { private String name; public Book(){ ...
- JavsScript中的Document对象
Document对象的属性 alinkColor,linkColor,vlinkColor:这些属性描述了超链接的颜色.linkColor指未访问过的链接的正常颜色,vlinkColor指访问过的链接 ...