26_promise.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // Promise 对象用作处理异步方法 它里面接受一个回调函数,回调函数内接受两个参数 第一个参数对应成功状态(then),第二个参数对应失败状态(cathc)
  11. // var pro1 = new Promise(function(resolve,reject){
  12. // setTimeout(function(){
  13. // resolve("成功了");
  14. // // reject();
  15. // },1000)
  16. // })
  17. // pro1.then(function(res){
  18. // console.log(res);
  19. // }).catch(function(){
  20. // console.log("失败了");
  21. // })
  22. // let p1 = new Promise(function (resolve, reject) {
  23. // setTimeout(function () {
  24. // resolve("p1成功")
  25. // }, 1000)
  26. // })
  27. // let p2 = new Promise(function (resolve, reject) {
  28. // setTimeout(function () {
  29. // // resolve("p2成功")
  30. // reject();
  31. // }, 2000)
  32. // })
  33. // let p3 = new Promise(function (resolve, reject) {
  34. // setTimeout(function () {
  35. // resolve("p1成功")
  36. // }, 3000)
  37. // })
  38. // Promise.all 接受一个参数 为数组 数组内部装有多个promise对象
  39. // 当内部的所有promise对象都成功的时候就会进入成功态 对应 then
  40. // 如果有一个失败就会进入失败状态 catch
  41. // Promise.all([p1,p2,p3]).then(function(res){
  42. // console.log(res)
  43. // }).catch(function(){
  44. // console.log("失败");
  45. // })
  46. // Promise.race 接受一个参数 为数组 数组内部装有多个promise对象
  47. // 表示赛跑 只要有一个promise对象成功 则进入到成功态,如果第一个就成功后面就不等了;
  48. // 如果全部失败 才进入失败状态 catch
  49. // Promise.race([p1, p2, p3]).then(function (res) {
  50. // console.log(res)
  51. // }).catch(function () {
  52. // console.log("失败");
  53. // })
  54. Promise.resolve().then(function(){
  55. console.log("成功");
  56. })
  57. Promise.reject().catch(function(){
  58. console.log("失败");
  59. })
  60. </script>
  61. </body>
  62. </html>