-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFROM-example.js
More file actions
53 lines (40 loc) · 1.7 KB
/
FROM-example.js
File metadata and controls
53 lines (40 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { from } from 'rxjs';
/*
-- from operator can be used to convert a promise to an observable
-- from operator emits all contained values of arrays and iterables as a sequence
-- from operator can also be used to emit a string as a sequence of characters
----------------------------------------------------------------------------------
from operator works in the following way:
-- create an observable instance
-- if the parameter is Array,
iterate over the source and emit array members as standalone values
-- if the parameter is Iterable,
iterate over the source and emit members as standalone values
-- if the source is Promise,
wait until the promise is resolved, and send the value to the observer.
if the promise is rejected, send the error notification to the observer
-- when there are no more values in the data source,
send the complete notification to the observer
-- from operator even works on an Observable-like objects,array etc
*/
const source = from("mukitul");
source.subscribe((val) => {
console.log("VALUE: ", val);
});
// thow error because from operator only reaceive array, promise, string or any iterables
// const objectSource = from({ name: "mukitul", address: "Dhaka" });
// objectSource.subscribe((val) => {
// console.log("VALUE: ", val);
// });
const apiCall = async () => {
return await new Promise((resolve, reject) => {
setTimeout(resolve, 2000, { name: "mukitul", address: "Dhaka" });
}).catch((err) => {
console.log("ERROR: ", err);
throw new Error("ERROR " + err);
})
}
const promiseSource = from(apiCall());
promiseSource.subscribe((val) => {
console.log("VALUE: ", val);
});