19.Promise.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. <!--
  10. Promise:构造函数
  11. 执行方向:自上到下
  12. 同步执行
  13. 异步执行
  14. 状态:三种状态
  15. 成功 resolve/fulfilled
  16. 失败 reject
  17. 等待 pending
  18. js执行顺序:
  19. 先同步 后异步(先微后宏)
  20. -->
  21. <script>
  22. // var a = 1;
  23. // console.log(a);
  24. // new Promise((resolve,reject)=>{
  25. // // 执行的代码块
  26. // reject();
  27. // console.log(12)
  28. // }).then(()=>{
  29. // // 执行成功的代码块
  30. // console.log(11)
  31. // }).catch(()=>{
  32. // // 执行失败的代码块
  33. // console.log(14)
  34. // })
  35. // console.log(10)
  36. let book1 = new Promise((resolve,reject)=>{
  37. console.log(1);
  38. reject();
  39. })
  40. let book2 = new Promise((resolve,reject)=>{
  41. console.log(2);
  42. reject();
  43. })
  44. let book3 = new Promise((resolve,reject)=>{
  45. console.log(3);
  46. reject();
  47. })
  48. // Promise.all() 判断多个Promise 当传入的都成立 则成立
  49. // Promise.all([book1,book2,book3]).then(()=>{
  50. // console.log('成功')
  51. // }).catch(()=>{
  52. // console.log('失败')
  53. // })
  54. // Promise.race() 判断多个Promise 当传入的第一个成立 则成立
  55. // Promise.race([book1,book2,book3]).then(()=>{
  56. // console.log('成功')
  57. // }).catch(()=>{
  58. // console.log('失败')
  59. // })
  60. Promise.allSettled([book1,book2,book3]).then(()=>{
  61. console.log('成功')
  62. }).catch(()=>{
  63. console.log('失败')
  64. })
  65. // book.then(()=>{
  66. // console.log("1")
  67. // })
  68. // book.catch(()=>{
  69. // console.log("12")
  70. // })
  71. </script>
  72. </body>
  73. </html>