|
@@ -0,0 +1,110 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <meta charset="UTF-8" />
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
+ <title>Document</title>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+ <script>
|
|
|
+ // async await 使用的是try{成功}catch{异常}
|
|
|
+ //await会阻塞后面的方法
|
|
|
+ // async function fn1() {
|
|
|
+ // console.log(1);
|
|
|
+ // await fn2();
|
|
|
+ // console.log(2);
|
|
|
+ // }
|
|
|
+ // async function fn2() {
|
|
|
+ // console.log("fn2");
|
|
|
+ // }
|
|
|
+ // fn2();
|
|
|
+ // console.log(3);
|
|
|
+
|
|
|
+ // 1 fn2 3 2
|
|
|
+
|
|
|
+ // async function async1() {
|
|
|
+ // console.log('async1 start')
|
|
|
+ // await async2()
|
|
|
+ // console.log('async1 end')
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('timer1')
|
|
|
+ // }, 300)
|
|
|
+ // }
|
|
|
+ // async function async2() {
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('timer2')
|
|
|
+ // }, 400)
|
|
|
+ // console.log('async2')
|
|
|
+ // }
|
|
|
+ // async1()
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('timer3')
|
|
|
+ // }, 200)
|
|
|
+ // console.log('start')
|
|
|
+
|
|
|
+ // async function fn1(){
|
|
|
+ // console.log(333)
|
|
|
+ // await fn2()
|
|
|
+ // console.log(111)
|
|
|
+ // }
|
|
|
+ // async function fn2(){
|
|
|
+ // throw new Error('rejected')
|
|
|
+ // }
|
|
|
+ // async function fn2(){
|
|
|
+ // console.log(4)
|
|
|
+ // }
|
|
|
+ // fn1()
|
|
|
+ // console.log(2222)
|
|
|
+
|
|
|
+ // async function fn1(){
|
|
|
+ // console.log(1)
|
|
|
+ // await fn2()
|
|
|
+ // console.log(2) // 微1
|
|
|
+ // setTimeout(()=>{ //宏1
|
|
|
+ // console.log(3)
|
|
|
+ // },1000)
|
|
|
+ // }
|
|
|
+ // async function fn2(){
|
|
|
+ // console.log(4)
|
|
|
+ // await fn3()
|
|
|
+ // console.log(5) //微2
|
|
|
+ // setTimeout(()=>{ //宏1
|
|
|
+ // console.log(31)
|
|
|
+ // },500)
|
|
|
+ // }
|
|
|
+ // async function fn3(){
|
|
|
+ // setTimeout(()=>{
|
|
|
+ // console.log(6) //宏2
|
|
|
+ // },2000)
|
|
|
+ // }
|
|
|
+ // fn1()
|
|
|
+ // console.log(7)
|
|
|
+
|
|
|
+ /***
|
|
|
+ * await 后 Promise时 进入微任务
|
|
|
+ * await 不是Promise 正常执行
|
|
|
+ // async function a1() {
|
|
|
+ // console.log(4)
|
|
|
+ // await a2();
|
|
|
+ // console.log(1)
|
|
|
+ // }
|
|
|
+ // async function a2() {
|
|
|
+ // await console.log(2);
|
|
|
+ // await console.log(7)
|
|
|
+ // }
|
|
|
+ // a1();
|
|
|
+ // async function async1() {
|
|
|
+ // await async2();
|
|
|
+ // console.log("async1");
|
|
|
+ // return "async1 success";
|
|
|
+ // }
|
|
|
+ // async function async2() {
|
|
|
+ // return new Promise((resolve, reject) => {
|
|
|
+ // console.log("async2");
|
|
|
+ // reject("error");
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ // async1().then((res) => console.log(res));
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|