简体   繁体   English

RxJS:Observable.create() 与 Observable.from()

[英]RxJS: Observable.create() vs. Observable.from()

What's the difference between these two?这两者有什么区别?

return Observable.create(function(observer) {
    if (array)
        observer.next([]);
    else
        observer.next(null);
    observer.complete();
});

and

return Observable.from( array ? [] : null );

I thought it could be the same but didn't work the same.我认为它可能是一样的,但没有一样工作。

The create(...) is a generic Observable factory method for creating an Observable in which you will explicitly dictate how values are passed to the Subscriber create(...)是用于创建Observable的通用Observable工厂方法,您将在其中明确规定如何将值传递给Subscriber

For instance, if you were to create a timer base Observable (don't it already exists as Observable.timer ) you could do:例如,如果您要创建一个基于计时器的Observable (它是否已经作为Observable.timer存在),您可以执行以下操作:

   Observable.create(observer => {
     const timeoutId = setTimeout(() => {
       observer.next(0);
       observer.complete();
     }, 500);

     return () => clearTimeout(timeoutId);
   });

The from(...) is what I call a conformance operator in that it attempts to coerce a passed in data type into an Observable (make it conform). from(...)是我所说的一致性运算符,因为它试图将传入的数据类型强制转换为Observable (使其符合)。 This means that it will accept a variety of types and convert them into Observables .这意味着它将接受各种类型并将它们转换为Observables These types include:这些类型包括:

  • Arrays数组
  • Promises承诺
  • Generators发电机
  • Observable-like things类似可观察的事物

There are specific converters as well that you can find such as fromArray and fromPromise which specifically convert those types, but from more of a swiss-army knife of those methods您还可以找到特定的转换器,例如fromArrayfromPromise专门转换这些类型,但更多的是from这些方法的瑞士军刀

If you just need a single value you should be using Observable.of (the docs appear to be out of date, just/return was renamed to of in RxJS 5 and I don't think they are aliased anymore).如果您只需要一个值,您应该使用Observable.of (文档似乎已经过时,在 RxJS 5 中将just/return重命名为of ,我认为它们不再是别名了)。

ie IE

// Don't quote me on the import part
import 'rxjs/add/observable/of';

Observable.of(1, 2, 3, 4).subscribe();

One difference is: Observable.from() specifically forbids null as something it accepts.一个区别是: Observable.from()特别禁止null作为它接受的东西。 So the first is kind of a workaround to create an observable returning null .所以第一个是创建一个返回null的可观察的解决方法。 (See the accepted answer for the main difference though.) (不过,请参阅已接受的答案以了解主要区别。)

Better (shorter) way would be to use just :更好(更短)的方式是使用

return Observable.just( array ? [] : null );

But I couldn't find where to get it or how to import it.但是我找不到从哪里获取它或如何导入它。

import 'rxjs/add/operator/just';

This isn't present in my distribution of rxjs.这在我的 rxjs 发行版中不存在。
Edit: just was renamed to of , see the accepted answer.编辑: just重命名为of ,请参阅已接受的答案。

When you are returning an array, once difference is that 'create/next' will emit the whole array at once but 'from' will emit each item as many times as its length, so to subscriber,当您返回一个数组时,区别在于 'create/next' 将立即发出整个数组,而 'from' 将发出每个项目的长度与其长度一样多,因此对于订阅者,

Observable.create(observer => observer.next(myArray)) will fire onNext event once to return myArray whereas Observable.from(myArray) will emit many onNext events to return one item each time. Observable.create(observer => observer.next(myArray))将触发 onNext 事件一次以返回 myArray 而Observable.from(myArray)将发出许多 onNext 事件以每次返回一个项目。

Observable.from(): Observable.from():
You have a list of values (that has nothing to do with Rx) and an Observable is created from this list.您有一个值列表(与 Rx 无关),并且该列表中创建了一个 Observable。 When an Observer subscribe to this Observable, the list of values are passed as Next() events one by one then Completed() is eventually passed and you cannot control this process.当 Observer 订阅此 Observable 时,值列表作为 Next() 事件一个一个地传递,然后 Completed() 最终传递,并且您无法控制此过程。

Observable.create(): Observable.create():
You create the sequence of event by explicitly emitting the events one by one.您可以通过一个一个显式地发出事件来创建事件序列。 Hence, you can emit OnCompleted and OnError events whenever you want因此,您可以随时发出 OnCompleted 和 OnError 事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM