method
pipe
function let's you chain RxJS operators:
12345678910111213const { 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'));
NOTE: pipe function returns a new Observable each time
factory
You can also use pipe
factory to create new operators:
12345678910111213141516171819const { 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 ) }