skip to content

pipe

 

method

pipe function let's you chain RxJS operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
const { rxObserver } = require('api/v0.3');
const { timer } = require('rxjs');
const { filter, take } = require('rxjs/operators');


timer(0, 10)
  .pipe(
    filter(x => x % 2), // filter only even numbers
    take(10)            // take 10 values and complete
  )
  .subscribe(rxObserver('Odd'));

0msOddstartcomplete11 33 55 77 99 1111 1313 1515 1717 1919

NOTE: pipe function returns a new Observable each time

factory

You can also use pipe factory to create new operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const { rxObserver } = require('api/v0.3');
const { timer, pipe } = require('rxjs');
const { filter, take } = require('rxjs/operators');


timer(0, 10)
  .pipe( // pipe operators
    tenEvens()
  )
  .subscribe(rxObserver('Odd'));

// create a new operator using pipe constructor
function tenEvens(){
  return pipe(
    filter(x => x % 2), // filter only even numbers
    take(10)            // take 10 values and complete
  )
} 
0msOddstartcomplete11 33 55 77 99 1111 1313 1515 1717 1919