22.练习.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 function fn1() {
  11. // console.log(1);
  12. // await fn2();
  13. // console.log(2);
  14. // }
  15. // async function fn2() {
  16. // console.log("fn2");
  17. // }
  18. // fn1()
  19. // fn2();
  20. // console.log(3);
  21. // async function async1() {
  22. // console.log('async1 start')
  23. // await async2()
  24. // console.log('async1 end')
  25. // setTimeout(() => {
  26. // console.log('timer1')
  27. // }, 300)
  28. // }
  29. // async function async2() {
  30. // setTimeout(() => {
  31. // console.log('timer2')
  32. // }, 400)
  33. // console.log('async2')
  34. // }
  35. // async1()
  36. // setTimeout(() => {
  37. // console.log('timer3')
  38. // }, 200)
  39. // console.log('start')
  40. // async function fn1(){
  41. // console.log(333)
  42. // await fn2()
  43. // console.log(111)
  44. // }
  45. // async function fn2(){
  46. // throw new Error('rejected')
  47. // }
  48. // async function fn2(){
  49. // console.log(4)
  50. // }
  51. // fn1()
  52. // console.log(2222)
  53. // async function fn1(){
  54. // console.log(1)
  55. // await fn2()
  56. // console.log(2)
  57. // setTimeout(()=>{
  58. // console.log(3)
  59. // },1000)
  60. // }
  61. // async function fn2(){
  62. // console.log(4)
  63. // await fn3()
  64. // console.log(5)
  65. // setTimeout(()=>{
  66. // console.log(31)
  67. // },500)
  68. // }
  69. // async function fn3(){
  70. // setTimeout(()=>{
  71. // console.log(6)
  72. // },2000)
  73. // }
  74. // fn1()
  75. // console.log(7)
  76. async function a1() {
  77. console.log(4)
  78. await a2();
  79. console.log(1)
  80. }
  81. async function a2() {
  82. await console.log(2);
  83. await console.log(7)
  84. }
  85. a1();
  86. async function async1() {
  87. await async2();
  88. console.log("async1");
  89. return "async1 success";
  90. }
  91. async function async2() {
  92. return new Promise((resolve, reject) => {
  93. console.log("async2");
  94. reject("error");
  95. });
  96. }
  97. async1().then((res) => console.log(res));
  98. </script>
  99. </body>
  100. </html>