Search⌘ K

Creating Observables From Javascript Data Types

Explore how to create Observables from various JavaScript data types such as arrays, events, and callbacks. Understand using operators like from and fromNodeCallback to transform data sources into Observables that enable reactive programming in RxJS.

Creating Observables from arrays

We can make any array-like object that can iterate into an Observable by using the versatile from operator. The from operator takes an array as a parameter and returns an Observable that emits each of its elements.

Javascript (babel-node)
Rx.Observable.from(['Adrià', 'Jen', 'Sergi']).subscribe(function(x)
{
console.log('Next: ' + x);
},
function(err)
{
console.log('Error:', err);
},
function()
{
console.log('Completed');
});

The from operator is, along with the fromEvent operator, one of the most convenient and frequently used operators in RxJS code.

Creating Observables from JavaScript events

When we transform an event into an ...