自学内容网 自学内容网

js手写-实现Promise的实例方法

catch

等效为Promise.prototype.then(undefined,onRejected)

 then(onFulfilled, onRejected) {

    return new MyPromise((resolve, reject) => {

        //定义默认的函数

        const defaultOnFulfilled = (value) => value;

        const defaultOnRejected = (reason) => {

          throw reason;

        }

        onFulfilled = onFulfilled || defaultOnFulfilled;

        onRejected = onRejected || defaultOnRejected;
//省略之后的代码
  

    });

  }

  catch(onRejected) {

   return this.then(undefined, onRejected);

  }

finally

   finally(onFinally) {

    this.then(onFinally, onFinally);

  }
  promise

  .then((val) => {

    console.log("then中的成功回调:", val);

  })

  .catch((err) => {

    console.log("catch中的失败回调结果", err);

  }).finally((e)=>{

      console.log("finally获得的结果",e);

  })

  ;

原文地址:https://blog.csdn.net/qq_73270720/article/details/145291558

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!