Differences between observable.takeUntil... and observable.pipe(takeUntil

Viewed 112

Can you explain me please what's the difference between observable.takeUntil and observable.pipe(takeUntil...?

Example:

this.api
        .testMethod(
            this.queryParam,
        )
        .takeUntil(this.destroy$)
        .subscribe(

this.api
        .testMethod(
            this.queryParam,
        )
        .pipe(takeUntil(this.destroy$))
        .subscribe(

When i write the first option, i'm seeing that i need import from 'rxjs/operators'...

1 Answers

It mostly boils down to the RxJS version used. RxJS v5 and earlier allowed chaining operators without the need for pipe. RxJS v6+ needs the pipe to be mentioned explicitly.

<= RxJS v5

import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/tap';

this.api.testMethod(this.queryParam)
  .takeUntil(this.destroy$)
  .map(data => data)
  .tap(console.log)
  .subscribe();

RxJS v6+

import { takeUntil, map, tap } from 'rxjs/operators';

this.api.testMethod(this.queryParam).pipe(
  takeUntil(this.destroy$),
  map(data => data),
  tap(console.log)
).subscribe();
Related