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. Android开源项目 Universal imageloader 源码研究之Lru算法

    https://github.com/nostra13/Android-Universal-Image-Loader universal imageloader 源码研究之Lru算法 LRU - Le ...

  2. JNI type

    ref: JNI type The mapping between the Java type and C type is: Type Signature Java Type Z boolean B ...

  3. show_space.sql.txt

    create or replace procedure SHOW_SPACE(P_SEGNAME   IN VARCHAR2, P_OWNER     IN VARCHAR2 DEFAULT USER ...

  4. 02-测试、文件读写、xml解析

    测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...

  5. nodejs概论(实操篇)

    什么是模块? 模块分为原生模块(node.jsAPI提供的原生模块,在启动时已经被加载)和 文件模块(动态加载模块,主要由原生模块module来实现和完成.通过调 用node.js的require方法 ...

  6. ggts下载地址

    地址:http://spring.io/tools/ggts See All Versions可以下载更多版本,里面包含安装版和解压版

  7. 封装好的PHP分页类,简单好用--在开源看到的,取回来自己用

    class Pagination  独立分页类 调用方式: $pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, ...

  8. CLR via C#可空值类型

    我们知道,一个值类型的变量永远不可能为null.它总是包含值类型本身.遗憾的是,这在某些情况下会成为问题.例如,设计一个数据库时,可将一个列定义成为一个32位的整数,并映射到FCL的Int32数据类型 ...

  9. Spring事务管理中@Transactional的propagation参数

    所谓事务传播性,就是被调用者的事务与调用者的事务之间的关系.举例说明. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 //in A.java Class A {     @Tr ...

  10. 如何让div出现滚动条

    由于DIV本身属性并没有滚动条设置,但是有些地方的设计却需要出现滚动条,如何实现呢?本人采用CSS样式来控制显示!而且也很简单,代码如下: <div style="OVERFLOW-Y ...