Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.
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.
(Observable
): An observable sequence that contains elements from the input sequence that satisfy the condition.
/* No Match */
var source = Rx.Observable.empty()
.first();
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Error: Error: Sequence contains no elements.
/* Without a predicate */
var source = Rx.Observable.range(0, 10)
.first();
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: 0
// => Completed
/* With a predicate */
var source = Rx.Observable.range(0, 10)
.first(function (x, idx, obs) { return x % 2 === 1; });
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: 1
// => Completed
File:
Dist:
Prerequisites:
NPM Packages:
NuGet Packages:
Unit Tests: