The of() operator essentially converted a list of arguments to an Observable. Since arrays are often how we structure list of things in JavaScript, we should have a way of transforming arrays into Observables. This lesson teaches how you can convert from Arrays to Observables, from Promises to Observables, and from Iterators to Observables.

formArray:

var ary = [,,];
var foo = Rx.Observable.fromArray(ary); foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
}); /*
"next 1"
"next 2"
"next 3"
"done"
*/

fromPromise:

var prom = fetch('https://null.jsbin.com');
var foo = Rx.Observable.fromPromise(prom); foo.subscribe(function (x) {
console.log('next ' + x.status);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
}); /*
"next 204"
"done"
*/

from:

from() opreator can create observalbes according to what you passed in

=fromArray:

var ary = [,,];
var foo = Rx.Observable.from(ary); foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
}); /*
"next 1"
"next 2"
"next 3"
"done"
*/

=fromPromiose

var foo = Rx.Observable.from(prom);

foo.subscribe(function (x) {
console.log('next ' + x.status);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});

From iterator:

function* generator(){
yield ;
yield ;
yield ;
} var iterator = generator();
var foo = Rx.Observable.from(iterator); foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
}); /*
"next 10"
"next 20"
"next 30"
"done"
*/

[RxJS] Creation operators: from, fromArray, fromPromise的更多相关文章

  1. [RxJS] Creation operators: interval and timer

    It is quite common to need an Observable that ticks periodically, for instance every second or every ...

  2. [RxJS] Creation operators: empty, never, throw

    This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...

  3. [RxJS] Creation operators: fromEventPattern, fromEvent

    Besides converting arrays and promises to Observables, we can also convert other structures to Obser ...

  4. [RxJS] Creation operator: of()

    RxJS is a lot about the so-called "operators". We will learn most of the important operato ...

  5. [RxJS] Creation operator: create()

    We have been using Observable.create() a lot in previous lessons, so let's take a closer look how do ...

  6. [RxJS] Transformation operators: debounce and debounceTime

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

  7. [RxJS] Transformation operators: delay and delayWhen

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

  8. [RxJS] Filtering operators: takeLast, last

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

  9. [RxJS] Filtering operators: take, first, skip

    There are more operators in the filtering category besides filter(). This lesson will teach how take ...

随机推荐

  1. java学习第一阶段——面向对象

    你聪明有人会说你心机重, 你靠的是努力有人会说你运气好, 你说自己天生乐观有人会说你虚假, 有时候, 你明明就是一杯白水, 却被人硬生生逼成了满肚子憋屈的碳酸饮料. 人一生要遇见太多人, 即使有些话字 ...

  2. CXF自动生成客户端

    官网下载apache-cxf-3.1.6,bin目录下.配置环境变量,生成客户端 http://blog.csdn.net/jaune161/article/details/25499939 http ...

  3. 认识div(division)在排版中的作用

    在网页制作过程过中,可以把一些独立的逻辑部分划分出来,放在一个<div>标签中,这个<div>标签的作用就相当于一个容器. 语法: <div>…</div&g ...

  4. cocos2d-x 实现沙子特效

    效果图. 说明. 标题取的不好, 原因是我也不知道这效果应该叫什么. 大概效果是这样的, 随着鼠标按下并且移动, 所到之处所有磁铁都将扩散. 就好比鼠标是一个圆形区域去排挤在场的磁铁. 话不多说, 直 ...

  5. CountDownLatch 和 CyclicBarrier 的运用及实现原理

    I.CountDownLatch 和 CyclicBarrier 的运用 CountDownlatch: 定义: 其是一个线程同步的辅助工具,通过它可以做到使一条线程一直阻塞等待,直到其他线程完成其所 ...

  6. 利用js加载本地图片预览功能

    直接上代码: 经测试,除safari6包括6以下不支持,其他均可正常显示. 原因:safari6不支持filereader,同时不能使用IE滤镜导致失效. fix: 可以利用canvas,解决safa ...

  7. Mysql 应该选择什么引擎

    对于如何选择存储引擎,可以简答的归纳为一句话:“除非需要用到某些INNODB 不具备的特性,并且没有其他办法可以替代,否则都应该选择INNODB 引擎”.例如:如果要用到全文索引,建议优先考虑INNO ...

  8. (转载)最实用的清除浮动代码 css的文字过长裁剪后面跟着省略号

    css: .clearfloat:after{display:block;clear:both;content:"";visibility:hidden;} .clearfloat ...

  9. 初涉JavaScript模式 (9) : 函数 【常用方式】

    回调模式 上一篇,对JavaScript函数进行了大体的介绍,这一篇对一些在工作中经常遇到的情况进行扩展. 在工作中,我们经常遇到很多需求,比如现在有一个需求: 一栋10层的大楼,当我们在坐电梯时,电 ...

  10. 通过 Xftp5 管理 centOS 7 文件

    一. 在安装好了centOS 7 的服务上,打开终端 运行 ip -s addr 命令 获取服务的IP地址 [root@localhost ~]# ip -s addr1: lo: <LOOPB ...