[RxJS] Combination operators: concat, startWith
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.
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"
*/
[RxJS] Combination operators: concat, startWith的更多相关文章
- RxSwift 系列(三) -- Combination Operators
RxSwift 系列(三) -- Combination Operators 前言 本篇文章将要学习如何将多个Observables组合成一个Observable. Combination Opera ...
- [RxJS] Combination operator: zip
CombineLatest and withLatestFrom are both AND-style combination operators. In this lesson, we will l ...
- Rxjs常用operators
本文使用的是angular6内置的rxjs,版本号为6.3.3 concat 通过顺序地发出多个 Observables 的值将它们连接起来,一个接一个的. 参数: 名称 类型 属性 描述 other ...
- [RxJS] Filtering operators: distinct and distinctUntilChanged
Operator distinct() and its variants are an important type of Filtering operator. This lessons shows ...
- [RxJS] Transformation operators: debounce and debounceTime
Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that the ...
- [RxJS] Transformation operators: delay and delayWhen
This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...
- [RxJS] Combination operator: withLatestFrom
Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...
- [RxJS] Combination operator: combineLatest
While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. ...
- [RxJS] Filtering operators: takeLast, last
Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...
随机推荐
- 理解Php中的Static
① 使用 static 可以将类中的成员标识为静态的,既可以用来标识成员属性,也可以用来标识成员方法,比如: <?php class China { public static $boy = 1 ...
- 精通 Oracle+Python,第 8 部分:适合 Oracle DBA 使用的 Python
传统上,当需要为操作系统编写一些脚本时,人们常常会选用 Bash 或 Perl 脚本工具.这些工具易于使用,因而它们几乎变得无处不在,渗透到了包括 Oracle Database 在内的其他软件中,O ...
- 如何在eclips下将一段代码抽取为方法Extract Method
最近读了读关于重构的文章,做了个小总结(在编程思想目录下<从文章"避免复制与粘贴"到文章"Extract Method"的反思 系列>). 然后因为 ...
- getInputStream与getReader方法
getInputStream 方法用于返回的一个代表实体内容的输入流对象,其类型为javax.servlet.ServletInputStream. getReader方法用于返回的一个代表实体内容的 ...
- CONCATENATION 引发的性能问题
背景是在一台11gR2的机器上,开发反映一个批处理比以前慢了3倍.经过仔细查看该SQL的执行计划,发现由于SQL中使用了or,导致CBO走出了一个非常糟糕的CONCATENATION路径. no_ex ...
- wpf 动画
1动画实现 通过控件的属性 RenderTransform 设置 (1)设置控件的变化类型,如平移变化,旋转变化等,变化起点. (2)根据属性值链接相应的动画类型,如简单动画,关键帧,路径动画以及故事 ...
- 【HDOJ】1043 Eight
这道题目最开始做的时候wa+TLE.后面知道需要状态压缩,最近A掉.并且练习一下各种搜索算法. 1. 逆向BFS+康拓展开. #include <iostream> #include &l ...
- BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 654 Solved: 4 ...
- Android Fragment实现分屏
在项目中碰到一个问题,新开发一个平板APP,项目要求是把原来的一个手机端APP放在项目左侧显示,右侧添加新加的功能. 首先想到了Fragment,以前做过Fragment的一些简单的Demo,但是都没 ...
- C#验证码
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...