187

Pipes are filters for transforming data (formats) in the template.

I came across the pipe() function as below. What does this pipe() function exactly mean in this case?

return this.http.get<Hero>(url)
  .pipe(
    tap(_ => this.log(`fetched hero id=${id}`)),
    catchError(this.handleError<Hero>(`getHero id=${id}`))
);
CharithJ
  • 46,289
  • 20
  • 116
  • 131
Dinesh Sharma
  • 2,117
  • 3
  • 13
  • 9

5 Answers5

224

Don't get confused with the concepts of Angular and RxJS

We have pipes concept in Angular and pipe() function in RxJS.

1) Pipes in Angular: A pipe takes in data as input and transforms it to the desired output
https://angular.io/guide/pipes

2) pipe() function in RxJS: You can use pipes to link operators together. Pipes let you combine multiple functions into a single function.

The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.
https://angular.io/guide/rx-library (search for pipes in this URL, you can find the same)

So according to your question, you are referring pipe() function in RxJS

Milo
  • 3,365
  • 9
  • 30
  • 44
Shiva Nayak Dharavath
  • 3,082
  • 1
  • 14
  • 19
56

The Pipes you are talking about in the starting description are different from the pipe you showed in the example.

In Angular(2|4|5) Pipes are used to format view as you said. I think you have a basic understanding of pipes in Angular, you can learn more about that from this link - Angular Pipe Doc

The pipe() you have shown in the example is the pipe() method of RxJS 5.5 (RxJS is the default for all Angular apps). In Angular5 all the RxJS operators can be imported using single import and they are now combined using the pipe method.

tap() - RxJS tap operator will look at the Observable value and do something with that value. In other words, after a successful API request, the tap() operator will do any function you want it to perform with the response. In the example, it will just log that string.

catchError() - catchError does exactly the same thing but with error response. If you want to throw an error or want to call some function if you get an error, you can do it here. In the example, it will call handleError() and inside that, it will just log that string.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
BhargavG
  • 896
  • 1
  • 6
  • 15
  • 1
    "Pipes are filters for transforming data (formats) in the template." Here he was talking about pipe in Angular 2+, like date, uppercase pipes provided in Angular(which exactly do what he said i.e. format data in the template) And in description he has shown example of RXJS pipe **function**. So yeah those 2 things are totally different. – BhargavG Apr 20 '19 at 04:31
  • 1
    I take it back, mea culpa. Overlooked that phrase. I wish I could undo the -1. :( But it's locked unfortunately. – bvdb Apr 20 '19 at 11:06
  • that is not a big deal. Happy that it clears all the doubts. Cheers :-) – BhargavG Apr 20 '19 at 11:17
36

Two very different types of Pipes Angular - Pipes and RxJS - Pipes

Angular-Pipe

A pipe takes in data as input and transforms it to a desired output. In this page, you'll use pipes to transform a component's birthday property into a human-friendly date.

import { Component } from '@angular/core';

@Component({
  selector: 'app-hero-birthday',
  template: `<p>The hero's birthday is {{ birthday | date }}</p>`
})
export class HeroBirthdayComponent {
  birthday = new Date(1988, 3, 15); // April 15, 1988
}

RxJS - Pipe

Observable operators are composed using a pipe method known as Pipeable Operators. Here is an example.

import {Observable, range} from 'rxjs';
import {map, filter} from 'rxjs/operators';

const source$: Observable<number> = range(0, 10);

source$.pipe(
    map(x => x * 2),
    filter(x => x % 3 === 0)
).subscribe(x => console.log(x));

The output for this in the console would be the following:

0

6

12

18

For any variable holding an observable, we can use the .pipe() method to pass in one or multiple operator functions that can work on and transform each item in the observable collection.

So this example takes each number in the range of 0 to 10, and multiplies it by 2. Then, the filter function to filter the result down to only the odd numbers.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
24

RxJS Operators are functions that build on the observables foundation to enable sophisticated manipulation of collections.

For example, RxJS defines operators such as map(), filter(), concat(), and flatMap().

You can use pipes to link operators together. Pipes let you combine multiple functions into a single function.

The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
manoj
  • 241
  • 2
  • 4
  • Do you have an example? – lofihelsinki Aug 21 '18 at 04:23
  • 4
    In the below example we have piped the filter and map function. Now both function will get executed sequentially as provided in example. First it will filter the result and then it will map the results. Hope it will help. import { filter, map } from 'rxjs/operators'; const squareOdd = of(1, 2, 3, 4, 5) .pipe( filter(n => n % 2 !== 0), map(n => n * n) ); // Subscribe to get values squareOdd.subscribe(x => console.log(x)); – manoj Aug 22 '18 at 06:11
10

You have to look to official ReactiveX documentation: https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md.

This is a good article about piping in RxJS: https://blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44.

In short .pipe() allows chaining multiple pipeable operators.

Starting in version 5.5 RxJS has shipped "pipeable operators" and renamed some operators:

do -> tap
catch -> catchError
switch -> switchAll
finally -> finalize