<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>async await 小练习</title>
</head>
<body>
</body>
</html>
<script>
// 使用 async await 将异步代码 变为同步代码
// 依次 打印 1 2 3
function fn1() {
return new Promise((res, rej) => {
setTimeout(() => {
res('2')
}, 3000)
})
}
async function fn() {
console.log('1');
b = await fn1()
console.log(b);
console.log(3);
}
fn()
// 不使用 async await 代码默认异步执行
// 依次 打印 1 3 2
function fn1() {
return new Promise((res, rej) => {
setTimeout(() => {
res('2')
}, 3000)
})
}
function fn() {
console.log('1');
b = fn1()
b.then(res=>{
console.log(res);
})
console.log(3);
}
fn()
</script>```
async await 小练习
最新推荐文章于 2023-04-17 09:32:28 发布