[RxJS] Combination operators: concat, startWith
2016-05-25 20:42
337 查看
Some Observables may complete, and we may want to append another Observable to the one which just completed. This lesson teaches you how to use the concat() operator for either appending or prepending, and how the shortcut operator startWith() is an easy way of prepending values to an Observable.
concat(observalbe): wait until the first observalbe complete than append the scond observable result.
Observable.concat(first$, second$): static way to concat:
The code return the same result as the code showing before.
startWith(value):
concat(observalbe): wait until the first observalbe complete than append the scond observable result.
var foo = Rx.Observable.interval(100).take(7); var more = Rx.Observable.of(10,11,12,13,14); /* --0--1--2--3--4--5--6--7-... take(7) (10,11,12,13,14|) (more) --0--1--2--3--4--5--6| (foo) concat --0--1--2--3--4--5--6|(10,11,12,13,14)| */ var bar = foo.concat(more); bar.subscribe( function (x) { console.log('next ' + x); }, function (err) { console.log('error ' + err); }, function () { console.log('done'); }, ); /* "next 0" "next 1" "next 2" "next 3" "next 4" "next 5" "next 6" "next 10" "next 11" "next 12" "next 13" "next 14" "done" */
Observable.concat(first$, second$): static way to concat:
var foo = Rx.Observable.interval(100).take(7); var more = Rx.Observable.of(10,11,12,13,14); /* --0--1--2--3--4--5--6--7-... take(7) (10,11,12,13,14|) (more) --0--1--2--3--4--5--6| (foo) concat --0--1--2--3--4--5--6|(10,11,12,13,14)| */ var bar = Rx.Observable.concat(foo, more); bar.subscribe( function (x) { console.log('next ' + x); }, function (err) { console.log('error ' + err); }, function () { console.log('done'); }, );
The code return the same result as the code showing before.
startWith(value):
var foo = Rx.Observable.interval(100).take(7); /* --0--1--2--3--4--5--6--7-... take(7) --0--1--2--3--4--5--6| (foo) startWith('more') ('more')--0--1--2--3--4--5--6| */ var bar = foo.startWith('more'); bar.subscribe( function (x) { console.log('next ' + x); }, function (err) { console.log('error ' + err); }, function () { console.log('done'); }, ); /* "next more" "next 0" "next 1" "next 2" "next 3" "next 4" "next 5" "next 6" "done" */
相关文章推荐
- 4类JavaScript内存泄露及如何避免
- js点击button按钮跳转到另一个新页面
- 小姿势--JS
- 2016年5月25日下午(妙味课堂js预热课程-4笔记二)
- JavaScript学习--Item12 undefined 与 null
- ajax 传json到后台servlet接收
- js 阻止冒泡排序
- JavaScript学习--Item11 arguments对象
- JavaScript学习--Item10 闭包(closure)
- JavaScript学习--Item8 函数,方法,构造函数调用 标签: javascript构造函数面向对象函数call
- js 获取文本框中光标所在字符串索引位置
- javascript变量作用域
- JavaScript学习--Item7 函数和(命名)函数表达式
- JavaScript学习--Item6 var预解析与函数声明提升(hoist )
- 图表 Echarts 3.0 简单使用
- 请用javascript求出1000以内所有的素数
- JavaScript学习--Item5 全局变量
- JavaScript学习--Item4 基本类型和基本包装类型(引用类型)
- JavaScript学习--Item3 隐式强制转换
- 一段js动态操作table代码