[RxJS] Refactoring CombineLatest to WithLatestFrom
2016-03-12 18:36
681 查看
This lesson shows why it’s preferable to using
Timer will continue until you enter the number in the input field:
In this case, withLatestFrom() works the same way:
But let's say we only want the timer log out 3 times then it should hit the complete block, logout "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.
withLatestFrominstead of
combineLatestin 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 <= 3) .filter((data)=> data.count === parseInt(data.text)) .reduce((acc, curr)=> acc + 1, 0) .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 <= 3) .filter((data)=> data.count === parseInt(data.text)) .reduce((acc, curr)=> acc + 1, 0) .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 <= 3) .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 + 1, 0) .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.
相关文章推荐
- JSP学习总结-servlet
- JavaScript模块化开发2
- 5.2 Javascript:循环-二维数组
- 关于JavaScript的内存分配
- 两个JSP页面跳转传参数
- JavaScript中循环遍历Array与Map的方法小结
- javascript 对象2
- JavaScript对表单验证的例子
- JS与OC交互(iPhone项目)中JavaScriptCore.framework的使用
- FancyBox的ajax利用json形成gallery
- 理解javascript中this关键字---转载
- jsp 初学01
- JavaScript中改变HTML样式
- JSP 介绍
- javascript--瀑布流
- js判断用户超时登录
- extjs6 入门 sencha cmd 新建项目
- json
- ext js 学习系列 二 MVC 框架的搭建 .
- 客户端js判断上传文件类型和文件大小