Rx.Observable.prototype.defaultIfEmpty([defaultValue])

Returns the elements of the specified sequence or the specified value in a singleton sequence if the sequence is empty.

Arguments

  1. [defaultValue=null] (Any): The value to return if the sequence is empty. If not provided, this defaults to null.

Returns

(Observable): An observable sequence that contains the specified default value if the source is empty; otherwise, the elements of the source itself.

Example

/* Without a default value */
var source = Rx.Observable.empty().defaultIfEmpty();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: null
// => Completed

/* With a defaultValue */
var source = Rx.Observable.empty().defaultIfEmpty(false);

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: false
// => Completed

Location

File:

Dist:

NPM Packages:

NuGet Packages:

Unit Tests:

results matching ""

    No results matching ""