123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // Promise 对象用作处理异步方法 它里面接受一个回调函数,回调函数内接受两个参数 第一个参数对应成功状态(then),第二个参数对应失败状态(cathc)
- // var pro1 = new Promise(function(resolve,reject){
- // setTimeout(function(){
- // resolve("成功了");
- // // reject();
- // },1000)
- // })
- // pro1.then(function(res){
- // console.log(res);
- // }).catch(function(){
- // console.log("失败了");
- // })
- // let p1 = new Promise(function (resolve, reject) {
- // setTimeout(function () {
- // resolve("p1成功")
- // }, 1000)
- // })
- // let p2 = new Promise(function (resolve, reject) {
- // setTimeout(function () {
- // // resolve("p2成功")
- // reject();
- // }, 2000)
- // })
- // let p3 = new Promise(function (resolve, reject) {
- // setTimeout(function () {
- // resolve("p1成功")
- // }, 3000)
- // })
- // Promise.all 接受一个参数 为数组 数组内部装有多个promise对象
- // 当内部的所有promise对象都成功的时候就会进入成功态 对应 then
- // 如果有一个失败就会进入失败状态 catch
- // Promise.all([p1,p2,p3]).then(function(res){
- // console.log(res)
- // }).catch(function(){
- // console.log("失败");
- // })
- // Promise.race 接受一个参数 为数组 数组内部装有多个promise对象
- // 表示赛跑 只要有一个promise对象成功 则进入到成功态,如果第一个就成功后面就不等了;
- // 如果全部失败 才进入失败状态 catch
- // Promise.race([p1, p2, p3]).then(function (res) {
- // console.log(res)
- // }).catch(function () {
- // console.log("失败");
- // })
- Promise.resolve().then(function(){
- console.log("成功");
- })
- Promise.reject().catch(function(){
- console.log("失败");
- })
- </script>
- </body>
- </html>
|