[RxJS] Starting a Stream with SwitchMap & switchMapTo
2016-03-08 03:34
579 查看
From an event map to another event we can use switchMap(), switchMap() accept an function which return an obervable.
The following code: When you click the button, it will start a interval to console out the count...
So the start$ switch map to a interval$ to avoid writting the nested subscribe function.
switchMap() actually is pretty useful when dealing with http event stream, it can help to cancel the previous http call.
switchMapTo(): which accept an observable:
Tow pieces of code, works the same way.
The following code: When you click the button, it will start a interval to console out the count...
const Observable = Rx.Observable; const startButton = document.querySelector('#start'); const start$ = Rx.Observable.fromEvent(startButton, 'click'); const interval$ = Observable.interval(400); const startInterval$ = start$.switchMap( () => { return interval$; }); startInterval$.subscribe( (x) => { console.log(x); });
So the start$ switch map to a interval$ to avoid writting the nested subscribe function.
switchMap() actually is pretty useful when dealing with http event stream, it can help to cancel the previous http call.
switchMapTo(): which accept an observable:
/* const startInterval$ = start$.switchMap( () => { return interval$; });*/ const startInterval$ = start$.switchMapTo( interval$ );
Tow pieces of code, works the same way.
相关文章推荐
- JavaScript--闭包
- Javascript小知识点
- js小技巧(持续更新)
- 理解javascript正则表达式
- javascript创建对象、对象继承的实用方式详解
- JS实现设置ff与ie元素绝对位置的方法
- js表单处理中单选、多选、选择框值的获取及表单的序列化
- js获取当前日期时间及其它日期操作汇总
- javascript对象的创建和访问
- 理解javascript函数式编程中的闭包(closure)
- 一道关于JavaScript变量作用域的面试题
- javascript html实现网页版日历代码
- js实现数组冒泡排序、快速排序原理
- 解决JSP的超链接中文乱码
- JavaScript 搜罗的问题
- yii1中指定js代码和文件在页面底部加载
- JSTL字符串处理函数
- js中通过函数名字符串调用函数
- 介绍一款js模版引擎handlebars
- 自定义EL表达式,将对象转成json格式,关键代码