Operators

Operators are what gives Observables their power. Without them they are nothing. There are close to 60+ operators

Let’s look at some:

of

  1. let stream$ = Rx.Observable.of(1,2,3,4,5)

Right here we are using a creation type operator to create an observable for us. This is synchronous in nature and outputs the values as soon as possible. Essentially it lets you specify what values to emit in a comma separated list.

do

  1. let stream$ =
  2. Rx.Observable
  3. .of(1,2,3)
  4. .do((value) => {
  5. console.log('emits every value')
  6. });

This is a very handy operator as it is used for debugging of your Observable.

filter

  1. let stream$ =
  2. Rx.Observable
  3. .of(1,2,3,4,5)
  4. .filter((value) => {
  5. return value % 2 === 0;
  6. })
  7. // 2,4

So this stops certain values from being emitted

Notice however that I can add the do operator in a handy place and can still investigate all the values

  1. let stream$ =
  2. Rx.Observable
  3. .of(1,2,3,4,5)
  4. .do((value) => {
  5. console.log('do',value)
  6. })
  7. .filter((value) => {
  8. return value % 2 === 0;
  9. })
  10. stream$.subscribe((value) => {
  11. console.log('value', value)
  12. })
  13. // do: 1,do : 2, do : 3, do : 4, do: 5
  14. // value : 2, 4