|
|
@@ -0,0 +1,257 @@
|
|
|
+<!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 可以理解成Promise的语法糖 把异步回调写法 改成同步写法
|
|
|
+ // async 一般写在函数前 await写在async函数中
|
|
|
+ // async function fn1() {
|
|
|
+ // return '你好';
|
|
|
+ // }
|
|
|
+ // function fn1() {
|
|
|
+ // return Promise.resolve('哈哈')
|
|
|
+ // }
|
|
|
+ // fn1().then(res => console.log(res))
|
|
|
+ // function fn2() {
|
|
|
+ // return new Promise((resolve, reject) => {
|
|
|
+ // setTimeout(() => resolve("你好"), 1000)
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ // async function fn3() {
|
|
|
+ // try {
|
|
|
+ // const result = await Promise.reject();
|
|
|
+ // console.log(result)
|
|
|
+ // } catch (err) {
|
|
|
+ // console.log(err)
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // fn3();
|
|
|
+ // fn2().then(res => {
|
|
|
+ // console.log(res)
|
|
|
+ // })
|
|
|
+ // async function a1() {
|
|
|
+ // console.log('a1 start');
|
|
|
+ // await a2();
|
|
|
+ // console.log('a1 end');
|
|
|
+ // }
|
|
|
+ // async function a2() {
|
|
|
+ // console.log('a2');
|
|
|
+ // }
|
|
|
+
|
|
|
+ // console.log('start');
|
|
|
+ // a1();
|
|
|
+ // console.log('end');
|
|
|
+
|
|
|
+ // console.log('同步1');
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('宏1');
|
|
|
+ // Promise.resolve().then(() => {
|
|
|
+ // console.log('微3');
|
|
|
+ // });
|
|
|
+ // });
|
|
|
+
|
|
|
+ // Promise.resolve().then(() => {
|
|
|
+ // console.log('微1');
|
|
|
+ // }).then(() => {
|
|
|
+ // console.log('微2');
|
|
|
+ // });
|
|
|
+ // console.log('同步2');
|
|
|
+
|
|
|
+ // console.log(1);
|
|
|
+ // async function f() {
|
|
|
+ // console.log(2);
|
|
|
+ // await Promise.resolve();
|
|
|
+ // console.log(3);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log(4);
|
|
|
+ // }, 0);
|
|
|
+
|
|
|
+ // f();
|
|
|
+ // console.log(5);
|
|
|
+
|
|
|
+ // async function fn1() {
|
|
|
+ // console.log('fn1 start');
|
|
|
+ // await fn2();
|
|
|
+ // console.log('fn1 end');
|
|
|
+ // }
|
|
|
+
|
|
|
+ // async function fn2() {
|
|
|
+ // console.log('fn2');
|
|
|
+ // Promise.resolve().then(() => {
|
|
|
+ // console.log('微任务');
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+
|
|
|
+ // console.log('开始');
|
|
|
+ // fn1();
|
|
|
+ // console.log('结束');
|
|
|
+
|
|
|
+ // console.log('s1');
|
|
|
+ // new Promise(resolve => {
|
|
|
+ // console.log('p1');
|
|
|
+ // resolve();
|
|
|
+ // }).then(() => {
|
|
|
+ // console.log('p2');
|
|
|
+ // setTimeout(() => console.log('h1'), 0);
|
|
|
+ // }).then(() => {
|
|
|
+ // console.log('p3');
|
|
|
+ // })
|
|
|
+
|
|
|
+ // console.log('s2');
|
|
|
+
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('宏');
|
|
|
+ // }, 0);
|
|
|
+
|
|
|
+ // async function test() {
|
|
|
+ // new Promise(resolve => {
|
|
|
+ // console.log('promise 同步');
|
|
|
+ // resolve();
|
|
|
+ // }).then(() => {
|
|
|
+ // console.log('微');
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+
|
|
|
+ // console.log('同步开头');
|
|
|
+ // test();
|
|
|
+ // console.log('同步结尾');
|
|
|
+
|
|
|
+ // console.log(1);
|
|
|
+ // async function a() {
|
|
|
+ // console.log(2);
|
|
|
+ // await b();
|
|
|
+ // console.log(6);
|
|
|
+ // }
|
|
|
+ // async function b() {
|
|
|
+ // console.log(3);
|
|
|
+ // Promise.resolve().then(() => {
|
|
|
+ // console.log(5);
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log(7);
|
|
|
+ // }, 0)
|
|
|
+ // a();
|
|
|
+ // console.log(4);
|
|
|
+
|
|
|
+
|
|
|
+ // start promise-exec fn-start end
|
|
|
+ // then1 fn-end
|
|
|
+ // set1 set1-micro1
|
|
|
+ // set2
|
|
|
+ // then2
|
|
|
+ // console.log('start');
|
|
|
+ // // 宏1
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('set1');
|
|
|
+ // Promise.resolve().then(() => {
|
|
|
+ // console.log('set1-micro1');
|
|
|
+ // });
|
|
|
+ // }, 0);
|
|
|
+ // new Promise((resolve) => {
|
|
|
+ // console.log('promise-exec');
|
|
|
+ // resolve();
|
|
|
+ // }).
|
|
|
+ // // 微1
|
|
|
+ // then(() => {
|
|
|
+ // console.log('then1');
|
|
|
+ // return new Promise(resolve => {
|
|
|
+ // // 宏2
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('set2');
|
|
|
+ // resolve();
|
|
|
+ // }, 0);
|
|
|
+ // });
|
|
|
+ // })
|
|
|
+ // // 第二轮
|
|
|
+ // .then(() => {
|
|
|
+ // console.log('then2');
|
|
|
+ // });
|
|
|
+ // async function fn() {
|
|
|
+ // console.log('fn-start');
|
|
|
+ // await Promise.resolve();
|
|
|
+ // // 微2
|
|
|
+ // console.log('fn-end');
|
|
|
+ // }
|
|
|
+ // fn();
|
|
|
+ // console.log('end');
|
|
|
+
|
|
|
+ // A B C D E K
|
|
|
+ // F G J
|
|
|
+ // H I
|
|
|
+ // console.log('A');
|
|
|
+ // async function foo() {
|
|
|
+ // console.log('B');
|
|
|
+ // await bar();
|
|
|
+ // // 微2
|
|
|
+ // console.log('G');
|
|
|
+ // }
|
|
|
+ // async function bar() {
|
|
|
+ // console.log('C');
|
|
|
+ // new Promise(resolve => {
|
|
|
+ // console.log('D');
|
|
|
+ // resolve();
|
|
|
+ // }).then(() => {
|
|
|
+ // // 微1
|
|
|
+ // console.log('F');
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ // // 宏1
|
|
|
+ // setTimeout(() => {
|
|
|
+ // console.log('H');
|
|
|
+ // Promise.resolve().then(() => {
|
|
|
+ // console.log('I');
|
|
|
+ // });
|
|
|
+ // }, 0);
|
|
|
+ // foo();
|
|
|
+ // new Promise(resolve => {
|
|
|
+ // console.log('E');
|
|
|
+ // resolve();
|
|
|
+ // }).then(() => {
|
|
|
+ // // 微3
|
|
|
+ // console.log('J');
|
|
|
+ // });
|
|
|
+ // console.log('K');
|
|
|
+
|
|
|
+ console.log('1');
|
|
|
+ setTimeout(() => {
|
|
|
+ console.log('8');
|
|
|
+ Promise.resolve()
|
|
|
+ .then(() => console.log('9'))
|
|
|
+ .then(() => console.log('10'));
|
|
|
+ }, 0);
|
|
|
+ new Promise(resolve => {
|
|
|
+ console.log('2');
|
|
|
+ resolve();
|
|
|
+ console.log('3');
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ console.log('4');
|
|
|
+ return Promise.resolve();
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ console.log('5');
|
|
|
+ setTimeout(() => {
|
|
|
+ console.log('11');
|
|
|
+ }, 0);
|
|
|
+ });
|
|
|
+ async function test() {
|
|
|
+ console.log('6');
|
|
|
+ await Promise.resolve();
|
|
|
+ console.log('7');
|
|
|
+ }
|
|
|
+ test();
|
|
|
+ console.log('最后同步');
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|