Debian Codegraph @

node/rxjs/src/internal/observable/from.ts

raw
1 import { Observable } from '../Observable';
2 import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
3 import { scheduled } from '../scheduled/scheduled';
4 import { innerFrom } from './innerFrom';
5
6 export function from<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>;
7 /** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled`. Details: https://rxjs.dev/deprecations/scheduler-argument */
8 export function from<O extends ObservableInput<any>>(input: O, scheduler: SchedulerLike | undefined): Observable<ObservedValueOf<O>>;
9
10 /**
11 * Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object.
12 *
13 * <span class="informal">Converts almost anything to an Observable.</span>
14 *
15 * ![](from.png)
16 *
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.
22 *
23 * ## Examples
24 *
25 * Converts an array to an Observable
26 *
27 * ```ts
28 * import { from } from 'rxjs';
29 *
30 * const array = [10, 20, 30];
31 * const result = from(array);
32 *
33 * result.subscribe(x => console.log(x));
34 *
35 * // Logs:
36 * // 10
37 * // 20
38 * // 30
39 * ```
40 *
41 * Convert an infinite iterable (from a generator) to an Observable
42 *
43 * ```ts
44 * import { from, take } from 'rxjs';
45 *
46 * function* generateDoubles(seed) {
47 * let i = seed;
48 * while (true) {
49 * yield i;
50 * i = 2 * i; // double it
51 * }
52 * }
53 *
54 * const iterator = generateDoubles(3);
55 * const result = from(iterator).pipe(take(10));
56 *
57 * result.subscribe(x => console.log(x));
58 *
59 * // Logs:
60 * // 3
61 * // 6
62 * // 12
63 * // 24
64 * // 48
65 * // 96
66 * // 192
67 * // 384
68 * // 768
69 * // 1536
70 * ```
71 *
72 * With `asyncScheduler`
73 *
74 * ```ts
75 * import { from, asyncScheduler } from 'rxjs';
76 *
77 * console.log('start');
78 *
79 * const array = [10, 20, 30];
80 * const result = from(array, asyncScheduler);
81 *
82 * result.subscribe(x => console.log(x));
83 *
84 * console.log('end');
85 *
86 * // Logs:
87 * // 'start'
88 * // 'end'
89 * // 10
90 * // 20
91 * // 30
92 * ```
93 *
94 * @see {@link fromEvent}
95 * @see {@link fromEventPattern}
96 *
97 * @param input A subscription object, a Promise, an Observable-like,
98 * an Array, an iterable, or an array-like object to be converted.
99 * @param scheduler An optional {@link SchedulerLike} on which to schedule the emission of values.
100 * @return An Observable converted from {@link ObservableInput}.
101 */
102 export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T> {
103 return scheduler ? scheduled(input, scheduler) : innerFrom(input);
104 }