10
0

28_async&await.html 751 B

123456789101112131415161718192021222324252627282930
  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 在函数前声明为异步处理函数
  11. // 在异步对象前使用await 等到异步对象执行完成之后 执行后续代码
  12. // await后必须是一个promise对象
  13. async function foo() {
  14. console.log("1")
  15. await new Promise(function(resolve,reject){
  16. setTimeout(function(){
  17. console.log(2)
  18. resolve()
  19. },1000)
  20. })
  21. console.log(3)
  22. }
  23. foo();
  24. </script>
  25. </body>
  26. </html>