Rx.Observable.from(iterable, [mapFn], [thisArg], [scheduler])

This method creates a new Observable sequence from an array-like or iterable object.

Arguments

  1. iterable (Array | Arguments | Iterable): An array-like or iterable object to convert to an Observable sequence.
  2. [mapFn] (Function): Map function to call on every element of the array.
  3. [thisArg] (Any): The context to use calling the mapFn if provided.
  4. [scheduler=Rx.Scheduler.currentThread] (Scheduler): Scheduler to run the enumeration of the input sequence on.

Returns

(Observable): The observable sequence whose elements are pulled from the given iterable sequence.

Example

// Array-like object (arguments) to Observable
function f() {
  return Rx.Observable.from(arguments);
}

f(1, 2, 3).subscribe(
  function (x) {
    console.log('Next: ' + x);
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });

// => Next: 1
// => Next: 2
// => Next: 3
// => Completed

// Any iterable object...
// Set
var s = new Set(['foo', window]);
Rx.Observable.from(s).subscribe(
  function (x) {
    console.log('Next: ' + x);
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });
// => Next: foo
// => Next: window
// => Completed

// Map
var m = new Map([[1, 2], [2, 4], [4, 8]]);
Rx.Observable.from(m).subscribe(
  function (x) {
    console.log('Next: ' + x);
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });
// => Next: [1, 2]
// => Next: [2, 4]
// => Next: [4, 8]
// => Completed

// String
Rx.Observable.from('foo').subscribe(
  function (x) {
    console.log('Next: ' + x);
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });
// => Next: f
// => Next: o
// => Next: o
// => Completed

// Using an arrow function as the map function to
// manipulate the elements
Rx.Observable.from([1, 2, 3], function (x) { return x + x; }).subscribe(
  function (x) {
    console.log('Next: ' + x);
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });
// => Next: 2
// => Next: 4
// => Next: 6
// => Completed

// Generate a sequence of numbers
Rx.Observable.from({length: 5}, function(v, k) { return k; }).subscribe(
  function (x) {
    console.log('Next: ' + x);
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });
// => Next: 0
// => Next: 1
// => Next: 2
// => Next: 3
// => Next: 4
// => Completed

Location

File:

Dist:

Prerequisites:

  • None

NPM Packages:

NuGet Packages:

Unit Tests:

results matching ""

    No results matching ""