-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-exec-seq.csl.ts
More file actions
87 lines (80 loc) · 1.8 KB
/
async-exec-seq.csl.ts
File metadata and controls
87 lines (80 loc) · 1.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 浏览器与Node的事件循环(Event Loop)有何区别?
// https://blog.csdn.net/Fundebug/article/details/86487117
// 关于async/await、promise和setTimeout执行顺序
// https://blog.csdn.net/yun_hou/article/details/88697954
// 1.
console.log('---------- 1. ----------')
async function async1() {
console.log("async1 start");
await async2();
console.log("async1 end");
}
async function async2() {
console.log("async2");
}
console.log("script start");
async1();
console.log("script end");
// 2.
await sleep(1000)
console.log('---------- 2. ----------')
console.log("script start");
let promise1 = new Promise(function (resolve) {
console.log("promise1");
resolve();
console.log("promise1 end");
}).then(function () {
console.log("promise2");
});
setTimeout(function () {
console.log("settimeout 1");
}, 2);
setTimeout(function () {
console.log("settimeout 2");
});
console.log("script end");
// 3.
await sleep(1000)
console.log('---------- 3. ----------')
const dateFrom = Date.now()
let i = 1;
const timer = setInterval(() => {
console.log(i++, Date.now() - dateFrom);
const s = Date.now();
while (Date.now() - s < 300) {}
}, 200);
setTimeout(() => {
clearInterval(timer);
}, 700);
// 4.
await sleep(1000)
console.log('---------- 4. ----------')
let p = [];
(function() {
setTimeout(() => {
console.log('timeout 0');
}, 0);
let i = 0;
for (; i < 3; i++) {
p[i] = function() {
return new Promise(function(resolve) {
console.log(`promise ${i}`);
resolve(`promise ${i * i}`);
})
}
}
})();
async function b() {
console.log('async -1');
}
function a() {
console.log(`async ${p.length}`);
return async function() {
console.log(`async ${p.length}`);
await b();
console.log('async -2')
};
}
p.push(a());
p[1]().then(console.log);
p[3]();