Skip to content

Commit 040ec2f

Browse files
pertrai1cartant
authored andcommitted
docs(from): Convert docs for from (#4031)
* docs(from): Convert docs for from * docs(from): updates from code review
1 parent 6e5f773 commit 040ec2f

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/internal/observable/from.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,90 @@ import { ObservableInput, SchedulerLike } from '../types';
1212

1313
export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T>;
1414
export function from<T>(input: ObservableInput<ObservableInput<T>>, scheduler?: SchedulerLike): Observable<Observable<T>>;
15+
16+
/**
17+
* Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object.
18+
*
19+
* <span class="informal">Converts almost anything to an Observable.</span>
20+
*
21+
* ![](from.png)
22+
*
23+
* `from` converts various other objects and data types into Observables. It also converts a Promise, an array-like, or an
24+
* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable" target="_blank">iterable</a>
25+
* object into an Observable that emits the items in that promise, array, or iterable. A String, in this context, is treated
26+
* as an array of characters. Observable-like objects (contains a function named with the ES2015 Symbol for Observable) can also be
27+
* converted through this operator.
28+
*
29+
* ## Examples
30+
* ### Converts an array to an Observable
31+
* ```javascript
32+
* import { from } from 'rxjs/observable/from';
33+
*
34+
* const array = [10, 20, 30];
35+
* const result = from(array);
36+
*
37+
* result.subscribe(x => console.log(x));
38+
*
39+
* // Logs:
40+
* // 10 20 30
41+
* ```
42+
*
43+
* ---
44+
*
45+
* ### Convert an infinite iterable (from a generator) to an Observable
46+
* ```javascript
47+
* import { take } from 'rxjs/operators';
48+
* import { from } from 'rxjs/observable/from';
49+
*
50+
* function* generateDoubles(seed) {
51+
* let i = seed;
52+
* while (true) {
53+
* yield i;
54+
* i = 2 * i; // double it
55+
* }
56+
* }
57+
*
58+
* const iterator = generateDoubles(3);
59+
* const result = from(iterator).pipe(take(10));
60+
*
61+
* result.subscribe(x => console.log(x));
62+
*
63+
* // Logs:
64+
* // 3 6 12 24 48 96 192 384 768 1536
65+
* ```
66+
*
67+
* ---
68+
*
69+
* ### with async scheduler
70+
* ```javascript
71+
* import { from } from 'rxjs/observable/from';
72+
* import { async } from 'rxjs/scheduler/async';
73+
*
74+
* console.log('start');
75+
*
76+
* const array = [10, 20, 30];
77+
* const result = from(array, async);
78+
*
79+
* result.subscribe(x => console.log(x));
80+
*
81+
* console.log('end');
82+
*
83+
* // Logs:
84+
* // start end 10 20 30
85+
* ```
86+
*
87+
* @see {@link fromEvent}
88+
* @see {@link fromEventPattern}
89+
* @see {@link fromPromise}
90+
*
91+
* @param {ObservableInput<T>} A subscription object, a Promise, an Observable-like,
92+
* an Array, an iterable, or an array-like object to be converted.
93+
* @param {SchedulerLike} An optional {@link SchedulerLike} on which to schedule the emission of values.
94+
* @return {Observable<T>}
95+
* @name from
96+
* @owner Observable
97+
*/
98+
1599
export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T> {
16100
if (!scheduler) {
17101
if (input instanceof Observable) {

0 commit comments

Comments
 (0)