29_async_await.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // async await
  11. // function foo(){
  12. // setTimeout(() => {
  13. // console.log("hello world");
  14. // }, 2000);
  15. // console.log("你好");
  16. // }
  17. // foo()
  18. // async await 语法糖
  19. // async 可以将行数改为异步函数
  20. // await 一定要写在 async 函数内 async/await一定要组合使用
  21. async function foo() {
  22. // 等待异步操作完成
  23. // 异步操作完成后,继续执行后续代码
  24. // await后边需要等待一个Promise对象
  25. await new Promise((resolve, reject) => {
  26. setTimeout(() => {
  27. console.log("hello world");
  28. resolve();
  29. }, 2000);
  30. })
  31. console.log("你好");
  32. }
  33. foo()
  34. </script>
  35. </body>
  36. </html>