defaultIfEmpty

signature: defaultIfEmpty(defaultValue: any): Observable

Emit given value if nothing is emitted before completion.

Examples

Example 1: Default for empty value

( jsBin | jsFiddle )

const empty = Rx.Observable.of();
//emit 'Observable.of() Empty!' when empty, else any values from source
const exampleOne = empty.defaultIfEmpty('Observable.of() Empty!');
//output: 'Observable.of() Empty!'
const subscribe = exampleOne.subscribe(val => console.log(val));
Example 2: Default for Observable.empty

( jsBin | jsFiddle )

//empty observable
const empty = Rx.Observable.empty();
//emit 'Observable.empty()!' when empty, else any values from source
const example = empty.defaultIfEmpty('Observable.empty()!');
//output: 'Observable.empty()!'
const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/defaultIfEmpty.ts

results matching ""

    No results matching ""