Rx.Observable.prototype.single([predicate], [thisArg], [defaultValue])
Rx.Observable.prototype.single([settings])
Returns a single element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists. If no default value is given, then onError
will be called. If there are more than one element that matches, then onError
will be called.
Arguments
Rx.Observable.prototype.single([predicate], [thisArg], [defaultValue])
[predicate]
(Function
): A predicate function to evaluate for elements in the source sequence. The callback is called with the following information:- the value of the element
- the index of the element
- the Observable object being subscribed
[thisArg]
(Any
): Object to use asthis
when executing the predicate.[defaultValue]
(Any
): Default value if no such element exists.
Rx.Observable.prototype.single([settings])
[settings]
(Object
): An object with the following fields[predicate]
(Function
): A predicate function to evaluate for elements in the source sequence. The callback is called with the following information:- the value of the element
- the index of the element
- the Observable object being subscribed
[thisArg]
(Any
): Object to use asthis
when executing the predicate.[defaultValue]
(Any
): Default value if no such element exists.
Returns
(Observable
): An observable sequence that contains elements from the input sequence that satisfy the condition.
Example
/* with a default value */
var source = Rx.Observable.empty().single({defaultValue: 42});
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});
// => Next: 42
// => Completed
/* With a predicate */
var source = Rx.Observable.range(0, 10)
.single(function (x, idx, obs) { return x === 1; });
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});
// => Next: 1
// => Completed
Location
File:
Dist:
Prerequisites:
NPM Packages:
NuGet Packages:
Unit Tests: