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. 三、singleton(单例化)一个对象的几种方法

    方案一:私有化构造器,通过static final域 @Test public void test13() { A215 a=A215.a; A215 b=A215.a; System.out.pri ...

  2. JAVA-4-斐波列

    public class Ch049 { public static void main(String[] args) { // TODO 自动生成的方法存根 int a = 1, b = 1; fo ...

  3. JavaScript-学习一加载不动

    为先加载的js后加载的html 加载完js运行时因为未加载html的原因导致找不到js所控制的元素 所以解决的方法就是把js放到控制元素的下方 或者html的底部 做成函数的时候可以放在头部,也就是说 ...

  4. jQuery中的阻止默认行为

    在很多元素中都存在默认行为,例如表单中的submit按钮,a标签等等.如果想要消除其中的默认行为,就需要一个事件event的方法来消除他们的默认行为. 这个方法就是event.preventDefau ...

  5. ibatis ORA-00911: 无效字符

    检查下xml文件中 sql的最后是不是写了 “;” 最容易犯这个毛病,都不知道吃了多少次亏了. 什么ORA-00911: 无效字符 什么The error occurred while applyin ...

  6. Entity Framework中实现指定字段更新

    foreach (var entity in databasePatents) { var patentTmp = sourcePClist.FirstOrDefault(p => p.Oid ...

  7. Spring Cp30配置

    1.配置db.properties <bean id= "propertyConfigurer" class="org.springframework.beans. ...

  8. eval("("+json对象+")")

    var obj=eval("("+data+")"); 看看下面这条,应该能想到json的数据结构“+(json对象名)+”由于json是以”{}”的方式来开始 ...

  9. CodeForces 474B(标记用法)

    CodeForces 474B Time Limit:1000MS Memory Limit:262144KB   64bit IO Format:%I64d & %I64u Descript ...

  10. python设计模式之装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...