RxJS - Creation Operator iif



This operator will decide which Observable will be subscribed.

Syntax

iif(condition: Function):Observable

Parameters

condition − The condition is a function if its return true the observable will be subscribed.

Return value

An observable will be returned based on the condition.

Example

import { iif, of } from 'rxjs';
import { mergeMap, first, last } from 'rxjs/operators';

let task1 = iif(
   () => (Math.random() + 1) % 2 === 0,
   of("Even Case"),
   of("Odd Case")
);
task1.subscribe(value => console.log(value));

iff() operator acts like a ternary operator and mostly used for if-else condition cases.

Output

Odd Case
Advertisements