Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Latest commit

 

History

History
71 lines (57 loc) · 2.5 KB

defaultifempty.md

File metadata and controls

71 lines (57 loc) · 2.5 KB

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: