The whole point with this category is to show how easy it is to create Observables from something so they can play nice with the operators and well whatever construct you come from enables rich composition.
In Rxjs4 there existed a bunch of operators with resembling names such as fromArray()
, from()
, fromPromise()
etc. All these from()
operators are now just from()
. Let's look at some examples:
old fromArray
import { from } from 'rxjs';
from([2,3,4,5])
**old fromPromise **
import { from } from 'rxjs';
from(new Promise(resolve, reject) => {
// do async work
resolve( data )
})
The of
operator takes x number of arguments so you can call it with one arguments as well as 10 arguments like so:
import { of } from 'rxjs';
of(1,2);
of(1,2,3,4);
There also exist a bunch of operators that allows you go the other way, i.e leave the wonderful world of observables and go back to a more primitive state like :
import { of } from 'rxjs';
let promise = of(1,2).toPromise();
promise.then(data => console.log('Promise', data));