2-await表达式
<!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>Document</title>
</head>
<body>
<script>
async function main() {
// 1.右侧为Promise对象成功的情况,返回值为成功的Promise状态的结果值
// let result = await new Promise((resolve, reject) => {
// // resolve("Ok");
// });
// 2.右侧为其他类型的数据
// let result2 = await 123;
// console.log(result2);
// 1.右侧为Promise对象失败的情况,会抛出错误,可以通过trycatch来捕获
try {
let result3 = await new Promise((resolve, reject) => {
reject("Error");
});
} catch (error) {
console.log(error);
}
}
main();
</script>
</body>
</html>