signature: from(ish: ObservableInput, mapFn: function, thisArg: any, scheduler: Scheduler): Observable

Turn an array, promise, or iterable into an observable.

:bulb: For arrays and iterables, all contained values will be emitted as a sequence!

:bulb: This operator can also be used to emit a string as a sequence of characters!


Example 1: Observable from array

( jsBin | jsFiddle )

//emit array as a sequence of values
const arraySource = Rx.Observable.from([1,2,3,4,5]);
//output: 1,2,3,4,5
const subscribe = arraySource.subscribe(val => console.log(val));
Example 2: Observable from promise

( jsBin | jsFiddle )

//emit result of promise
const promiseSource = Rx.Observable.from(new Promise(resolve => resolve('Hello World!')));
//output: 'Hello World'
const subscribe = promiseSource.subscribe(val => console.log(val));
Example 3: Observable from collection

( jsBin | jsFiddle )

//works on js collections
const map = new Map();
map.set(1, 'Hi');
map.set(2, 'Bye');

const mapSource = Rx.Observable.from(map);
//output: [1, 'Hi'], [2, 'Bye']
const subscribe = mapSource.subscribe(val => console.log(val));
Example 4: Observable from string

( jsBin | jsFiddle )

//emit string as a sequence
const source = Rx.Observable.from('Hello World');
//output: 'H','e','l','l','o',' ','W','o','r','l','d'
const subscribe = source.subscribe(val => console.log(val));

Additional Resources

:file_folder: Source Code:

results matching ""

    No results matching ""