-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-el-observable.js
57 lines (46 loc) · 1.04 KB
/
7-el-observable.js
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
54
55
56
57
const { Observable, of } = rxjs;
// Create new macro-task
setTimeout(() => {
console.log("timeout"); // 10
});
// Create new micro-task
Promise.resolve().then(() => {
console.log("promise-one"); // 7
});
console.log("code-one"); // sync code
// Create new macro-task
const promise = new Promise((resolve) => {
resolve();
console.log("resolve"); // sync coe
});
// Exec promise
promise.then(() => {
console.log("promise-two");
});
// Create new observable (sync code)
const stream = new Observable((subscriber) => {
console.log("observable"); // sync code
subscriber.next();
});
// sync code
stream.subscribe(() => {
console.log("subscribe");
});
console.log("code-two"); // sync code
// 'of' is empty and it won't execute
of().subscribe(() => {
console.log("of-subscribe"); // sync code
});
// Create new micro-task
Promise.resolve().then(() => {
console.log("promise-three");
});
// 1 - code-one
// 2 - resolve
// 3 - observable
// 4 - subscribe
// 5 - code-two
// 6 - promise-one
// 7 - promise-two
// 8 - promise-three
// 9 - timeout