1 import { Observable } from '../Observable';
2 import { subscribeTo } from '../util/subscribeTo';
3 import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
4 import { scheduled } from '../scheduled/scheduled';
6 export function from<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>;
7 /** @deprecated use {@link scheduled} instead. */
8 export function from<O extends ObservableInput<any>>(input: O, scheduler: SchedulerLike): Observable<ObservedValueOf<O>>;
11 * Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object.
13 * <span class="informal">Converts almost anything to an Observable.</span>
17 * `from` converts various other objects and data types into Observables. It also converts a Promise, an array-like, or an
18 * <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable" target="_blank">iterable</a>
19 * object into an Observable that emits the items in that promise, array, or iterable. A String, in this context, is treated
20 * as an array of characters. Observable-like objects (contains a function named with the ES2015 Symbol for Observable) can also be
21 * converted through this operator.
25 * ### Converts an array to an Observable
28 * import { from } from 'rxjs';
30 * const array = [10, 20, 30];
31 * const result = from(array);
33 * result.subscribe(x => console.log(x));
43 * ### Convert an infinite iterable (from a generator) to an Observable
46 * import { from } from 'rxjs';
47 * import { take } from 'rxjs/operators';
49 * function* generateDoubles(seed) {
53 * i = 2 * i; // double it
57 * const iterator = generateDoubles(3);
58 * const result = from(iterator).pipe(take(10));
60 * result.subscribe(x => console.log(x));
77 * ### With async scheduler
80 * import { from, asyncScheduler } from 'rxjs';
82 * console.log('start');
84 * const array = [10, 20, 30];
85 * const result = from(array, asyncScheduler);
87 * result.subscribe(x => console.log(x));
99 * @see {@link fromEvent}
100 * @see {@link fromEventPattern}
102 * @param {ObservableInput<T>} A subscription object, a Promise, an Observable-like,
103 * an Array, an iterable, or an array-like object to be converted.
104 * @param {SchedulerLike} An optional {@link SchedulerLike} on which to schedule the emission of values.
105 * @return {Observable<T>}
109 export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T> {
111 if (input instanceof Observable) {
114 return new Observable<T>(subscribeTo(input));
116 return scheduled(input, scheduler);