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.

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的更多相关文章

  1. RxSwift 系列(三) -- Combination Operators

    RxSwift 系列(三) -- Combination Operators 前言 本篇文章将要学习如何将多个Observables组合成一个Observable. Combination Opera ...

  2. [RxJS] Combination operator: zip

    CombineLatest and withLatestFrom are both AND-style combination operators. In this lesson, we will l ...

  3. Rxjs常用operators

    本文使用的是angular6内置的rxjs,版本号为6.3.3 concat 通过顺序地发出多个 Observables 的值将它们连接起来,一个接一个的. 参数: 名称 类型 属性 描述 other ...

  4. [RxJS] Filtering operators: distinct and distinctUntilChanged

    Operator distinct() and its variants are an important type of Filtering operator. This lessons shows ...

  5. [RxJS] Transformation operators: debounce and debounceTime

    Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that the ...

  6. [RxJS] Transformation operators: delay and delayWhen

    This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...

  7. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  8. [RxJS] Combination operator: combineLatest

    While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. ...

  9. [RxJS] Filtering operators: takeLast, last

    Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...

随机推荐

  1. AS3.0函数定义的方法

    在AS3.0中函数的定义有两种方法: 函数语句定义法: function 函数名(参数1:参数类型,参数2:参数类型):返回值类型{ 函数折行的语句 } function testAdd(a:int, ...

  2. js实现中文简繁切换效果

    html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  3. #Leet Code# Same Tree

    语言:Python 描述:使用递归实现 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # ...

  4. C#中的泛型-1

    在软件这个行业,做的越久,往往会觉得很多技术问题最终会偏向数据结构和算法. 记得曾经大学的一堂课上,老师讲了一个关于冒泡排序的算法,下面是课本上的标准实现. public class Sort { p ...

  5. 我学C的那些年[ch02]:宏,结构体,typedef

    c语言的编译过程: 预处理 编译 汇编 链接 而预处理中有三种情况: 文件包含( #include ) 条件编译(#if,#ifndef,#endif) 宏定义( #define ) 宏就是预处理中的 ...

  6. 关于CMD命令行两三事

    1.返回盘符:

  7. Android 开发遇到的问题及解决办法

    Failed to resolve: com.android.support:appcompat-v7:23.4.0 问题解决办法: 1.在Android SDK Manager中找到对应的SDK版本 ...

  8. iOS 中KVC、KVO、NSNotification、delegate 总结及区别-b

    1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...

  9. JDK源码阅读(一) ArrayList

    基于JDK7.0 ArrayList<E>类继承了抽象类AbstractList<E> 实现了List<E> 接口,RandomAccess接口,Cloneable ...

  10. NEURAL NETWORKS, PART 2: THE NEURON

    NEURAL NETWORKS, PART 2: THE NEURON A neuron is a very basic classifier. It takes a number of input ...