123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <!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 await
- * async 在函数前添加async使函数变成异步 默认状态成功
- * await 不仅局限于使用在async里 后面跟表达式 如果和async一起使用 await后的内容 属于微任务
- */
-
- // async await 使用的是try{成功}catch{异常}
- //await会阻塞后面的方法
-
- /***
- * await 后 Promise时 进入微任务
- * await 不是Promise 正常执行
- * */
- // async function fn1() {
- // return 111;
- // }
- // fn1().then(val=>{
- // console.log(val,'val')
- // });
- // async function fn2() {
- // throw new Error("reject");
- // }
- // console.log(fn2());
- // fn1();
- // console.log(222);
- function count(num) {
- return new Promise((resolve,reject)=>{
- setTimeout(()=>{
- resolve(num * 2);
- },1000)
- })
- }
- async function fn3() {
- let total1 = await count(2);
- let total2 = await count(20);
- let total3 = await count(12);
- console.log(total1,total2,total3);
- }
- fn3();
- console.log("333")
-
- </script>
- </body>
- </html>
-
|