[RxJS] Creation operators: from, fromArray, fromPromise
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的更多相关文章
- [RxJS] Creation operators: interval and timer
It is quite common to need an Observable that ticks periodically, for instance every second or every ...
- [RxJS] Creation operators: empty, never, throw
This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...
- [RxJS] Creation operators: fromEventPattern, fromEvent
Besides converting arrays and promises to Observables, we can also convert other structures to Obser ...
- [RxJS] Creation operator: of()
RxJS is a lot about the so-called "operators". We will learn most of the important operato ...
- [RxJS] Creation operator: create()
We have been using Observable.create() a lot in previous lessons, so let's take a closer look how do ...
- [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] Filtering operators: takeLast, last
Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...
- [RxJS] Filtering operators: take, first, skip
There are more operators in the filtering category besides filter(). This lesson will teach how take ...
随机推荐
- Mac OS X 10.9 Mavericks 修改root密码
Mac10.9忘记密码后有两种方式可以进去: 代码如下 复制代码 1.sudo passwd 重新输入密码即可,此方法修改了root的密码 代码如下 复制代码 2.sudo bash 输入当前用户 ...
- 强引用,弱引用,4种Java引用浅解(涉及jvm垃圾回收)
http://www.jb51.net/article/49085.htm http://www.jb51.net/article/49085.htm
- ueditor 1.4.3.2 独立/单独 上传图片框原理
其实简单的说就是编辑框很多按钮,所有按钮的功能都是以command形式提供,所以execCommand就是执行这些功能的命令.有些按钮是能弹出显示一个对话框,他的基类就是dialog,而所有被弹出的d ...
- [HttpClient]HttpClient简介
1. 前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java应用程序需要直接通过 HTTP 协议来访问网路资源.虽然在 JDK 的 java net包中已 ...
- matplotlib入门--1(条形图, 直方图, 盒须图, 饼图)
作图首先要进行数据的输入,matplotlib包只提供作图相关功能,本身并没有数据读入.输出函数,针对各种试验或统计文本数据输入可以使用numpy提供的数据输入函数. # -*- coding: gb ...
- UVA 1594 Ducci Sequence(两极问题)
Ducci Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- Swift互用性:与 Objective-C 的 API 交互(Swift 2.0版更新)-备
本页包含内容: 初始化 可失败初始化 访问属性 方法 id 兼容性(id Compatibility) 空值和可选值 扩展(Extensions) 闭包(Closures) 比较对象 Swift 类型 ...
- iOS的REST服务-备
REST式的服务最重要的三个特征就是**无状态性**(statelessness).**统一资源定位**(uniform resource identification)和**可缓存性**(cache ...
- android 休眠唤醒机制分析(一) — wake_lock
本文转自:http://blog.csdn.net/g_salamander/article/details/7978772 Android的休眠唤醒主要基于wake_lock机制,只要系统中存在任一 ...
- 转:Zend_Cache的使用
一.Zend_Cache快速浏览 Zend_Cache 提供了一个缓存任何数据的一般方法. 在Zend Framework中缓存由前端操作,同时通过后端适配器(File, Sqlite, Memcac ...