32-异步方案与Node
码路教育 9/13/2001
# 一. 下面程序输入结果是?
<script>
console.log("script start")
function requestData(url) {
console.log("requestData")
return new Promise((resolve) => {
setTimeout(() => {
console.log("setTimeout")
resolve(url)
}, 2000);
})
}
async function getData() {
console.log("getData start")
const res = await requestData("wc")
console.log("then1-res:", res)
console.log("getData end")
}
getData()
console.log("script end")
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 二. 下面程序输入结果是?
<script>
console.log("script start")
setTimeout(function() {
console.log("setTimeout1");
new Promise(function(resolve) {
resolve();
}).then(function() {
new Promise(function(resolve) {
resolve();
}).then(function() {
console.log("then4");
});
console.log("then2");
});
});
new Promise(function(resolve) {
console.log("promise1");
resolve();
}).then(function() {
console.log("then1");
});
setTimeout(function() {
console.log("setTimeout2");
});
console.log(2);
new Promise(function(resolve) {
resolve();
}).then(function() {
console.log("then3");
});
console.log("script end")
</script>
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
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
# 三. 下面程序输入结果是?
async function async1() {
console.log('async1 start')
await async2()
console.log('async1 end')
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(function() {
console.log('setTimeout0')
}, 0)
setTimeout(function() {
console.log('setTimeout2')
}, 300)
async1();
new Promise(function(resolve) {
console.log('promise1')
resolve();
console.log('promise2')
}).then(function() {
console.log('promise3')
})
console.log('script end')
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
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
# 四. 下面程序输入结果是?
<script>
async function async1() {
console.log('async1 start')
await async2();
console.log('async1 end')
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(function() {
console.log('setTimeout')
}, 0)
async1();
new Promise(function(resolve) {
console.log('promise1')
resolve();
}).then(function() {
console.log('promise2')
})
console.log('script end')
</script>
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
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
# 五. 下面程序输入结果是?(选做)
- 自行研究
<script>
Promise.resolve().then(() => {
console.log(0);
return {
then: function(resolve, reject) {
resolve(4)
}
};
}).then((res) => {
console.log(res)
})
Promise.resolve().then(() => {
console.log(1);
}).then(() => {
console.log(2);
}).then(() => {
console.log(3);
}).then(() => {
console.log(5);
}).then(() => {
console.log(6);
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 六. 下面程序输入结果是?(选做)
- 自行研究
<script>
Promise.resolve().then(() => {
console.log(0);
return Promise.resolve(4);
}).then((res) => {
console.log(res)
})
Promise.resolve().then(() => {
console.log(1);
}).then(() => {
console.log(2);
}).then(() => {
console.log(3);
}).then(() => {
console.log(5);
}).then(() => {
console.log(6);
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20