Skip to content

Latest commit

 

History

History
110 lines (88 loc) · 3.47 KB

single.md

File metadata and controls

110 lines (88 loc) · 3.47 KB

Rx.Observable.prototype.single([predicate], [thisArg])

Returns the only element of an observable sequence that satisfies the condition in the optional predicate, and reports an exception if there is not exactly one element in the observable sequence.

Arguments

  1. [predicate] (Function): A predicate function to evaluate for elements in the source sequence. The callback is called with the following information:
    1. the value of the element
    2. the index of the element
    3. the Observable object being subscribed
  2. [thisArg] (Any): Object to use as this when executing the predicate.

Returns

(Observable): Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.

Example

/* No Match */
var source = Rx.Observable.empty()
    .single();

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.return(42)
    .single();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + 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: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 1
// => Completed

/* More than one match */
var source = Rx.Observable.range(0, 10)
    .single(function (x, idx, obs) { return x % 2 === 0; });

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

// => Error: Error: Sequence contains more than one element'

Location

File:

Dist:

Prerequisites:

NPM Packages:

NuGet Packages:

Unit Tests: