Learn RxJS
Search…
⌃K

iif

signature: iif(condition: () => boolean, trueResult: SubscribableOrPromise = EMPTY, falseResult: SubscribableOrPromise = EMPTY): Observable

Subscribe to first or second observable based on a condition

Examples

Example 1: simple iif
// 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
// 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);
Example 3: iif with default
// RxJS v6+
import { fromEvent, iif, of, interval, pipe } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
interval(1000)
.pipe(
mergeMap(v =>
iif(
() => !!(v % 2),
of(v)
// if not supplied defaults to EMPTY
)
)
// output: 1,3,5...
)
.subscribe(console.log);

Additional Resources

  • iif 📰 - Official docs
Last modified 2yr ago