Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 2.62 KB

single.md

File metadata and controls

75 lines (59 loc) · 2.62 KB

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

Returns the first element of an observable sequence that satisfies the condition in the predicate, or undefined if no such element exists.

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): An observable sequence that contains elements from the input sequence that satisfy the condition.

Example

/* Without a predicate */
var source = Rx.Observable.empty().single();

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

// => Next: undefined
// => 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: