Thursday, 23 April 2020

RxJs Pipe Operator


RxJS is a framework for reactive programming that makes use of Observables, making it really easy to write asynchronous code.

Sujeet Bhujbal Angular

What is Pipe Operator

RxJS pipe is used to combine functional operators into a chain. 
pipe is an instance method of Observable as well as a standalone RxJS function.
 pipe can be used as Observable.pipe or we can use standalone pipe to combine functional operators.

 pipe accepts operators as arguments such as filtermapmergeScan etc. with comma separated and execute them in a sequence they are passed in as arguments and finally returns Observable instance.

 To get the result we need to subscribe the final Observable response. 

Please find the sample code below

of(1, 2, 3, 4, 5, 6, 7).pipe(
    filter(n => n % 2 === 1),
    map(n => n + 10)
.subscribe(result => console.log(result)); 

Output will be 11, 13, 15, 17.

Happy programming!!
Don’t forget to leave your feedback and comments below!

Sujeet Bhujbal