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

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

问题描述

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

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

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

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

getProfile() {//这是对 REST API 的调用返回 this.service.getProfile().map(res => res.json()).take(1)}}this.data.getProfile().subscribe(profile => {this.userProfile = 个人资料;});

解决方案

作者订阅后没有退订.是不是因为作者使用 take(1) 因此不需要手动取消订阅?

是的,这很可能是作者使用 take(1) 运算符的原因.您可以在这里阅读更多关于 take 运算符的信息.它的工作是将一个值传递给可观察对象,然后从源中取消订阅.但根据服务的不同,可能不需要.

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

@Injectable()导出类 HttpXhrBackend 实现 HttpBackend {...句柄(请求:HttpRequest):可观察的>{...//一切都发生在 Observable 订阅上.return new Observable((observer: Observer<HttpEvent<any>>) => {...//首先是加载事件,它表示响应完全可用.常量 onLoad = () =>{...如果(好){//在事件流上传递成功的响应.观察者.next(新的 HttpResponse({身体,标头,地位,状态文本,网址:网址||不明确的,}));//完整的 body 已经接收和传递,没有进一步的事件//是可能的.此请求已完成.观察者.完成();<------------------------------------}

<块引用>

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

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

const stream = Observable.create((observer) => {观察者错误(新错误());}).take(1).catch(() => {return Observable.of(`发生错误`);}).subscribe((v) => {控制台.log(v);//发生错误})

I have seen a tutorial on rxjs as like this.

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) The author did not unsubscribe after subscribe. Is it because the author use take(1) therefore manual unsubscribe is not needed?

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;
});

解决方案

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

Yes, that is most likely why the author uses take(1) operator. You can read more about take operator here. Its 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.

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();   <---------------------------------
        }

What if i want to implement catch function. Should i implement it before take or after 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天全站免登陆