What is exact difference between pipe and map in angular 7?

Viewed 7443

I have searched a lot about this but i am unable to find difference between pipe and map in angular 7? Is it necessary to use pipe in Service.ts file in angular 7?

Thanks

3 Answers

With rxjs 5.5 and above, if you want to use any operator on an observable you just pipe them. So here map is just one of those operators within the pipe.

ex:

const example = source.pipe(map(val => val + 10), first());

For earlier versions of rxjs there was no pipe keyword, Multiple operators were combined using . notation

ex:

const example = source.map(val => val + 10).first();

map(), filter(), concat(), and flatMap() these are the operators offered by the RxJS library. You can use pipes to link these operators together. Pipes let you combine multiple functions into a single function as follows:

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

const squareOddVals = pipe(
filter((n: number) => n % 2 !== 0),
map(n => n * n)
);

for short explanation, pipe in RxJS, is used to intercept the result and modify it using RxJS operators so that when you subscribe it you will get the final result equivalent to the logic operators you set in the pipe example map, tap and many more.

for angular practice guide check it here https://angular.io/guide/rx-library

you can see the list of operators here https://www.learnrxjs.io/operators/

Related