Rxjs:take(1)用法 [英] Rxjs: take(1) usage

查看:765
本文介绍了Rxjs:take(1)用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这样的关于rxjs的教程.

I have seen a tutorial on rxjs as like this.

我的问题是:
1)take(1)的用途是什么?我在网上看到很多解释,但我真的不明白.另外,我看不到在此代码中使用take(1)有任何好处.而且作者确实在与REST api相关的服务的每个返回函数上使用take(1).

My question is:
1) What is the usage of take(1) here? I see a lot of explanation online but i don't really get it. Also, i don't see any benefit of using take(1) in this code. And the author do use take(1) on every return function in REST api related service.

2)作者在订阅后并未取消订阅.是不是因为作者使用take(1),所以不需要手动退订?

2) The author did not unsubscribe after subscribe. Is it because the author use take(1) therefore manual unsubscribe is not needed?

3)如果我想实现catch功能怎么办.我应该在服用前还是服用后实施它.

3) What if i want to implement catch function. Should i implement it before take or after take.

getProfile() { // this is a call to REST API
    return this.service.getProfile()
            .map(res => res.json())
            .take(1)
    }
}

this.data.getProfile().subscribe(profile => {
    this.userProfile = profile;
});

推荐答案

作者在订阅后并未取消订阅.是因为 作者使用take(1),因此不需要手动退订吗?

The author did not unsubscribe after subscribe. Is it because the author use take(1) therefore manual unsubscribe is not needed?

是的,这很可能是作者使用take(1)运算符的原因.它的工作是将一个值传递给可观察值,然后从源退订.但是根据服务的不同,它可能不是必需的.

Yes, that is most likely why the author uses take(1) operator. It's job is to pass one value to an observable and then unsubscribe from the source. But depending on the service it may not be required.

例如,在Angular中,HttpClient服务在发送最终的HttpResponse事件值后自行完成流,因此您既不需要使用take也不明确地取消订阅. 这是来源:

For example in Angular the HttpClient service completes the stream by itself after sending the final HttpResponse event value so you don't need to neither use take nor unsubscribe explicitly. Here is the sources:

@Injectable()
export class HttpXhrBackend implements HttpBackend {
  ...
  handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
    ...
    // Everything happens on Observable subscription.
    return new Observable((observer: Observer<HttpEvent<any>>) => {
      ...
      // First up is the load event, which represents a response being fully available.
      const onLoad = () => {
        ...
        if (ok) {
          // A successful response is delivered on the event stream.
          observer.next(new HttpResponse({
            body,
            headers,
            status,
            statusText,
            url: url || undefined,
          }));
          // The full body has been received and delivered, no further events
          // are possible. This request is complete.
          observer.complete();   <---------------------------------
        }

如果我想实现catch功能怎么办.我应该在服用前还是服用后实施它.

What if i want to implement catch function. Should i implement it before take or after take.

您可以在take之后实现它,因为take也会传输错误.

You can implement it after take since take will also transmit the error.

const stream = Observable.create((observer) => {
  observer.error(new Error());
}).take(1).catch(() => {
  return Observable.of(`An error occurred`);
}).subscribe((v) => {
  console.log(v);  // An error occurred
})

这篇关于Rxjs:take(1)用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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