RxJS - Conditional Operator defaultIfEmpty



This operator will return a default value if the source observable is empty.

Syntax

defaultIfEmpty(defaultValue = null): Observable

Parameters

defaultValue − The argument defaultValue can be given some value or if not given it is null by default.

Return value

It will return an observable with a default value if the source observable is empty.

Example

import { of } from 'rxjs';
import { defaultIfEmpty } from 'rxjs/operators';

let list1 = of();
let final_val = list1.pipe(defaultIfEmpty('Empty! No values'));
final_val.subscribe(x => console.log(x));

Output

Empty! No values
Advertisements