[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 ...
随机推荐
- 【转】 iOS 原生二维码扫描(可限制扫描区域)
在用 AVFoundation 完成扫码后,遇到2个问题: 1,如何限制扫描范围? 2.条形码如何扫描? 一位朋友的文章帮助了我,特地转来,可以帮到有需要的朋友. 原文:http://blog.csd ...
- wpf 控件复制 克隆
方法1: string xaml = System.Windows.Markup.XamlWriter.Save(rtb1); RichTextBox rtb2 =System.Windows.Mar ...
- JavaScript设计模式之建造者模式
一.建造者模式模式概念 建造者模式可以将一个复杂的对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示.也就是说如果我们用了建造者模式,那么用户就需要指定需要建造的类型就可以得到它们,而具体 ...
- 关于超链接自动提示的demo
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- libcurl 下载上传
近来一个新的项目需要使用到http. 本来用socket来写一个的,后来发现功能实在太简单,有点捉襟见肘. 于是改用libcur来做. 首先下载libcur的源码,然后配置: ./configure ...
- How to use PhotoshopApplication in maxscript
未完待续 ps_app= createOLEObject "Photoshop.Application" ps_app.Load "d:\\test\\aaa.tga&q ...
- HIVE json格式数据的处理
今天要处理一个以json格式存储的数据,想要直接把json的各个项的数据存入HIVE表中. HIVE直接读入json的函数有两个: (1)get_json_object(string json_str ...
- js blog
http://www.csdn.net/article/2013-12-16/2817820-javascript-survey-results 近日DailyJS社区发起了一项针对JavaScrip ...
- 桌面小部件----LED电子时钟实现
桌面控件是通过 Broadcast 的形式来进行控制的,因此每个桌面控件都对应于一个BroadcastReceiver.为了简化桌面控件的开发,Android 系统提供了一个 AppWidgetPro ...
- 7.1.1.关闭WebSocket连接
7.1.定义 7.1.1.关闭WebSocket连接 为_关闭WebSocket连接_,端点需关闭底层TCP连接.端点应该使用一个方法完全地关闭TCP连接,以及TLS会话,如果合适,丢弃任何可能已经接 ...