js中promise的使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [1,2,3,4,5];
        arr.forEach((item)=>{
            console.log('settimeout之前');
            setTimeout(function(){
            console.log(item);
            },3000);
            console.log('settimeout之后');
        })
        console.log('promise');
    </script>
</body>
</html>
//index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:13 settimeout之前
//index.html:17 settimeout之后
/index.html:13 settimeout之前
//index.html:17 settimeout之后
//index.html:19 promise
//index.html:15 1
//index.html:15 2
//index.html:15 3
//index.html:15 4
//index.html:15 5




 <script>
        try {
          let data ;
          console.log(data.a);
      } catch (error) {
          console.log(error);
      }
    </script>





 <script>
        try {
            let data = "errorData";
            throw new Error(data);
            console.log('data是否继续执行');
        } catch (error) {
            console.log('输出data错误信息');
            console.log(error);
        }
        try {
            let data1 = "errorData1";
            throw (data1);
            console.log('data1是否继续执行');
        } catch (error) {
            console.log('输出data错误信息');
            console.log(error);
        }
    </script>
//index.html:18 输出data错误信息
//index.html:19 Error: errorData
//    at index.html:15
//index.html:26 输出data错误信息
//index.html:27 errorData1


       let promise = new Promise((resolve,reject)=>{
            console.log('进入promise');
            let data = 'promsie-data';
            resolve(data);
            // reject(data);
            console.log('resove和reject之后');
        }).then((res)=>{
            console.log('resolve');
            console.log('进入then');
            console.log(res);
            console.log('返回res');
            return res;
        }).catch((err)=>{
            console.log('reject');
            console.log('输出catch')
            console.log(err);
            console.log('返回err');
            return err;
        });

        console.log('出了promsie');
        promise.then((res)=>{
            console.log('promise出来接着then');
            console.log(res);
        })
        console.log('结束');

    </script>
//index.html:14 进入promise
//index.html:18 resove和reject之后
//index.html:33 出了promsie
//index.html:38 结束
//index.html:20 resolve
//index.html:21 进入then
//index.html:22 promsie-data
//index.html:23 返回res
//index.html:35 promise出来接着then
//index.html:36 promsie-data




 <script>
        let promise = new Promise((resolve,reject)=>{
            console.log('进入promise');
            let data = 'promsie-data';
            // resolve(data);
            reject(data);
            console.log('resove和reject之后');
        }).then((res)=>{
            console.log('resolve');
            console.log('进入then');
            console.log(res);
            console.log('返回res');
            return res;
        }).catch((err)=>{
            console.log('reject');
            console.log('输出catch')
            console.log(err);
            console.log('返回err');
            return err;
        });

        console.log('出了promsie');
        promise.then((res)=>{
            console.log('promise出来接着then');
            console.log(res);
        })
        console.log('结束');

    </script>
//index.html:14 进入promise
//index.html:18 resove和reject之后
//index.html:33 出了promsie
//index.html:38 结束
//index.html:26 reject
//index.html:27 输出catch
//index.html:28 promsie-data
//index.html:29 返回err
//index.html:35 promise出来接着then
//index.html:36 promsie-data



Promise.all()
Promise.allSettled()
Promise.any()
Promise.prototype.catch()
Promise.prototype.finally()
Promise.race()
Promise.reject()
Promise.resolve()
Promise.prototype.then()

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    (
      function (window) {

        function Promise(excutor) {
          const self = this;
          self.status = 'pending';
          self.data = undefined;
          self.callbacks = [];
          function resolve(value) {
            if (self.status !== 'pending') {
              return
            }
            self.status = 'resolved';
            self.data = value;
            if (self.callbacks.length > 0) {
              setTimeout(() => {
                self.callbacks.forEach(callbacksObj => {
                  callbacksObj.onResolved(value);
                });
              });
            }
          }
          function reject(res) {
            if (self.status !== 'pending') {
              return
            }
            self.status = 'rejected';
            self.data = res;
            if (self.callbacks.length > 0) {
              setTimeout(() => {
                self.callbacks.forEach(callbacksObj => {
                  callbacksObj.onRejected(value);
                });
              });
            }
          }
          try {
            excutor(resolve, reject);
          } catch (error) {
            reject(error);
          }
        }
        Promise.prototype.then = function (onResolved, onRejected) {
          onResolved = typeof onResolved === 'function' ? onResolved : value => value
          onRejected = typeof onRejected === 'function' ? onRejected : res => { throw res }
          const self = this;

          return new Promise((resolve, reject) => {
            function handle(callback) {
              try {
                const result = onResolved(self.data);
                if (result instanceof Promise) {
                  // result.then(value => {
                  //   resolve(value)
                  // }, res => {
                  //   reject(res)
                  // })
                  result.then(resolve, reject)
                }
                else {
                  resolve(result);
                }
              } catch (error) {
                reject(error)
              }
            }
            if (self.status === "pending") {
              self.callbacks.push({
                onResolved() {
                  handle(onResolved)
                }, onRejected() {
                  handle(onRejected)
                }
              })
            }
            if (self.status === "resolved") {
              setTimeout(() => {
                handle(onResolved)
              })
            }
            if (self.status === "rejected") {
              setTimeout(() => {
                handle(onRejected)
              })
            }
          })
        }
        Promise.prototype.catch = function (onRejected) {
          return this.then(undefined, onRejected)
        }
        Promise.resolve = function (value) {

        }
        Promise.reject = function (res) {

        }
        Promise.all = function (promises) {

        }
        Promise.race = function (promises) {

        }
        window.Promise = Promise;
      }
    )(window)
    let p = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(2);
        console.log('reject');
      }, 100)
    }).then(value => {
      console.log('value', value);
      console.log('value');
    }, res => {
      console.log('res');
      console.log('res:', res)
    })
    // p.then(value => {
    //   console.log('value1', value);
    //   console.log('value1');
    // }, res => {
    //   console.log('res1:', res)
    // })
  </script>
</body>

</html>

promise+setTimeout调用

let p = new Promise(resolve => {
      setTimeout(function () {
      resolve('succ')
      },1000);
      console.log('conso');
      resolve('conso-----scc')
    },reject=>{
      reject('fail')
    });
    p.then((d)=>{
      alert(d);
    }).catch(e=>{
      console.log(e);
    })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度一下吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值