O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating an observable from arbitrary arguments

You have already learned how to create an observable from a sequence using the from() method, but there is a way to create an observable from an arbitrary sequence of arguments. We can do this using the of() method.

This method creates an observable that emits each argument passed to it.

The of() method has the following signature:

Rx.Observable.of(...args); 

The three dots before args illustrate an arbitrary number of arguments.

  • ...args: This refers to any number of objects

The following code shows an implementation of the of() method:

Rx.Observable     .of(0,1,2)     .subscribe((i)=>console.log(i)); 

If you run this code, you will see the following output:

    0    1    2

Now let's see what happens ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required