Returns the elements of the specified sequence or the specified value in a singleton sequence if the sequence is empty.
[defaultValue=null]
(Any
): The value to return if the sequence is empty. If not provided, this defaults to null.
(Observable
): An observable sequence that contains the specified default value if the source is empty; otherwise, the elements of the source itself.
/* 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
File:
Dist:
NPM Packages:
NuGet Packages:
Unit Tests: