"new Observable()" 和有什么区别?和“of()"在 RxJ 中 [英] what is the difference between "new Observable()" and "of()" in RxJs

查看:52
本文介绍了"new Observable()" 和有什么区别?和“of()"在 RxJ 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RxJs 中的 new Observable()of() 有什么区别?

What is the difference between new Observable() and of() in RxJs?

在我的测试用例中,当我尝试返回 new Observable() 时,它给了我一个有线错误,如果我用 Rxjs 的 of() 替换它,它就可以工作很好.

In my test cases when I try to return new Observable() it gives me a wired error and if I replace it with the of() from Rxjs it works fine.

我的印象是 Observable.create()new Observable()of() 做同样的事情.

My impression was Observable.create(), new Observable() and of() does same thing.

someService.getMethod().pipe(
  ...
  catchError((e) => {
    // some operation
    // return new Observable(false as any);  // ----> creates error
    return of(false as any); // this works perfectly
  })
)

推荐答案

Observable.create()new Observable() 基本上做同样的事情.

Observable.create() and new Observable() essentially do the same thing.

来自 Observable 的剖析:

Rx.Observable.createObservable constructor 的别名,它接受一个参数:subscribe函数.

Rx.Observable.create is an alias for the Observable constructor, and it takes one argument: the subscribe function.

Observable.of,另一方面是Observable上的static方法.它为您创建了一个 Observable,它会立即一个接一个地发出您指定为参数的值,然后发出一个完整的通知.

Observable.of, on the other hand is a static method on Observable. It creates an Observable for you, that emits value(s) that you specify as argument(s) immediately one after the other, and then emits a complete notification.

您的实施问题:

您对自定义 Observable 的实现是错误的.当你 new 一个 Observable 时,你必须传入一个 subscribe 函数给它的 constructor 有一个 observer作为 arg.这个 observer 上有诸如 nexterrorcomplete 之类的方法,这些方法在该 observable 的生命周期.

Your implementation of a Custom Observable is wrong. When you new up an Observable, you have to pass in a subscribe function to its constructor which has an observer as an arg. This observer has methods like next, error, and complete on it which gets called at those particular instances in the lifecycle of that observable.

您还应该公开一个具有 unsubscribe 方法的 Subscription 对象,然后消费者可以使用该方法进行任何清理.

You should also expose a Subscription Object that has an unsubscribe method on it, which can then be used by the consumer for doing any clean-up.

以下是它的一般实施方式:

const yourCustomObservable = new Observable((observer) => {
  observer.next("This pushes new value to the consumer");
  observer.error("This pushes an error to the consumer");
  observer.complete();

  return function unsubscribe() {
    // When the consumer unsubscribes, clean up data ready for next subscription.
  };
});

对于您的特定用例,您可以使用:

new Observable(...):

import { Observable } from 'rxjs';
...
someService.getMethod().pipe(
  ...
  catchError((e) => {
    // some operation
    return new Observable(observer => observer.next(false));
  })
)

Observable.create:

import { Observable } from 'rxjs';
...
someService.getMethod().pipe(
  ...
  catchError((e) => {
    // some operation
    return Observable.create(observer => observer.next(false));
  })
)

of:

import { of } from 'rxjs';
...
someService.getMethod().pipe(
  ...
  catchError((e) => {
    // some operation
    return of(false);
  })
)

来自:

import { from } from 'rxjs';
...
someService.getMethod().pipe(
  ...
  catchError((e) => {
    // some operation
    return from([false]);
  })
)

看看这个参考示例 StackBlitz.

何时使用什么?

new Observable(...)Observable.create(...) 为您提供更精细的控制,您可以在其中定义自己的 subscribe 功能并在其中做任何您想做的事情.因此,您可能希望使用它来实现通常无法使用 Observable 上提供的 static 方法创建的自定义 Observable.但是,对于像您这样的简单用例,使用 Observable.ofObservable.from 就足够了.

new Observable(...) or Observable.create(...) gives you a more fine grain control where you can define your own subscribe function and do whatever you want in it. So you might want to use it for implementing custom Observables that can't be generally created using the static methods provided on Observable. For simple use cases like yours, however, using Observable.of or Observable.from for that matter, would suffice.

这篇关于"new Observable()" 和有什么区别?和“of()"在 RxJ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆