iif

signature: iif<T, F>(condition: () => boolean, trueResult: SubscribableOrPromise<T> = EMPTY, falseResult: SubscribableOrPromise<F> = EMPTY): Observable<T | F>

Decides at subscription time which Observable will actually be subscribed.

Examples

Example 1: simple iif

( Stackblitz )

// RxJS v6+
import { iif, of, interval } from 'rxjs';
import { mergeMap } from 'rxjs/operators';

const r$ = of('R');
const x$ = of('X');

interval(1000).pipe(
  mergeMap(v =>
    iif(
      () => v % 4 === 0,
      r$,
      x$
    ))
).subscribe(console.log);

//output: R, X, X, X, R, X, X, X, etc...
Example 2: iif with mouse moves

( Stackblitz )

// RxJS v6+
import { fromEvent, iif, of } from 'rxjs';
import { mergeMap, map, throttleTime, filter } from 'rxjs/operators';

const r$ = of(`I'm saying R!!`);
const x$ = of(`X's always win!!`);

fromEvent(document, 'mousemove').pipe(
  throttleTime(50),
  filter((move: MouseEvent) => move.clientY < 210),
  map((move: MouseEvent) => move.clientY),
  mergeMap(yCoord =>
    iif(
      () => yCoord < 110,
      r$,
      x$
    ))
).subscribe(console.log);

Additional Resources

  • iif :newspaper: - Official docs

:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/iif.ts

results matching ""

    No results matching ""