Rxjs: take(1) usage

南笙酒味 提交于 2019-12-05 06:40:30

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. 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.

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
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!