[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验证码?
			<?php /** *制作验证码 *1.启动session *2.设定标头 *3.创建画布 *4.创建颜色 *5.创建随机数并放到画布上 *6.将得到的若干随机数放入session中 *7.添加 ... 
- Winbind authentication against active directory
			Winbind authentication against active directory Description This tip will describe how to configure ... 
- dp 0-1背包问题
			0-1背包的状态转换方程 f[i,j] = Max{ f[i-1,j-Wi]+Pi( j >= Wi ), f[i-1,j] } f[i,j]表示在前i件物品中选择若干件放在承重为 j 的背包 ... 
- 学习Swift -- 可选链
			可空链式调用 可空链式调用是一种可以请求和调用属性.方法及下标的过程,它的可空性体现于请求或调用的目标当前可能为空(nil).如果可空的目标有值,那么调用就会成功:如果选择的目标为空(nil),那么这 ... 
- sleep与wait的区别,详细解答(通过代码验证)
			package com.ysq.test; /** * sleep与wait的区别: * @author ysq * */ public class SleepAndWait { public sta ... 
- jQuery Mobile 控制 select 的显示隐藏 display none
			如需要动态控制下拉选择菜单select的显隐,一般考虑使用display:none这个方法. 但在jQueryMobile中的select添加自定义的css,display:none 是无效的. 解决 ... 
- [LeetCode#271] Encode and Decode Strings
			Problem: Design an algorithm to encode a list of strings to a string. The encoded string is then sen ... 
- tomcat  配置内存相关
			今天早上 ,tomcat 网站页面上出现报错问题.最后还是一位同事解决的,这里记录一下. 1.看了一下页面,他说是内存溢出. 首先找到 双击 Tomw.exe 出现如下图 然后需要配置堆栈大小 
- STM32F072B-DISCO 深入研究  中断系统
			STM32F072B-DISCO 是我认为性价比最高的一款CPU的demo系统,以前一直在用PIC的CPU但最近几年ST异军突起,几次课题查找芯片无一例外都是ST,像USB,CAN,ZIGBEE等,S ... 
- iOS类的继承关系
