微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

promise A+ 规范

<!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>promise A+ 规范</title>
  </head>
  <body>
    <script>
      let a = `每一个异步场景都可以看做是一个任务,这个任务分为两个阶段 三个状态
                unsettled 未决阶段   settled 已决阶段 pending 等待  fulfilled 成功态 rejected 失败态`;
      let b = `各个阶段和各个状态间不可逆转,状态始终是由 pending => fulfilled 或 rejected`;
      let c = `将任务由未决的 pending 状态推向已决的fulfilled状态,这个行为叫做 resolve;推向已决的rejected状态,这个行为叫 reject;`;
      let d = `每个异步任务必须提供一个then方法来处理成功后的数据或失败后的原因;then方法规定了有两个参数, onFulfilled onRejected`;
    </script>
    <script>
      const PENDING = 'pending';
      const FULFILLED = 'fulfilled';
      const REJECTED = 'rejected';
      // promise是一个类,他接受一个函数作为参数
      class myPromise {
        constructor(executor) {
          this.status = PENDING;
          this.dispatchers = [];
          this.data = null;
          this.reason = null;
          try {
            executor(this.__resolve.bind(this), this.reject.bind(this));
          } catch (err) {
            this.__reject(err);
          }
        }
        __updataStatus(newStatus) {
          if (this.status !== PENDING) return;
          this.status = newStatus;
        }
        __resolve(data) {
          this.__updataStatus(FULFILLED);
          this.data = data;
          this.__executedispatchers();
        }

        __executedispatchers({ dispatcher, status }) {
          if (this.status === PENDING) return;
          for (const dispatcherConf of dispatchers) {
            this.__executedispatcher(dispatcherConf);
          }
        }

        __executedispatcher({ dispatcher, status, resolve, reject }) {
          if (this.status !== status) {
            return;
          }
          if (typeof dispatcher !== 'function') {
            this.status === FULFILLED ? resolve(this.data) : reject(this.data);
            return;
          }
          // dispatcher已经是一个函数了
          try {
            const result = dispatcher(this.status === FULFILLED ? this.data : this.reason); // 如果是fulfilled 我们就把 data 传进去,如果是 rejected 我们就要把 reason 传进去;
            resolve(result);
          } catch (err) {
            reject(err);
          }
        }
        __reject(reason) {
          this.__updataStatus(REJECTED);
          this.reason = reason;
          this.__executedispatchers();
        }
        then(onFulfilled, onRejected) {
          return new myPromise((resolve, reject) => {
            this.dispatchers.push(
              {
                status: FULFILLED,
                dispatcher: onFulfilled,
                reject,
                resolve
              },
              {
                status: REJECTED,
                dispatcher: onRejected,
                reject,
                resolve
              }
            );
          });
        }
        catch(onRejected) {
          return this.then(null, onRejected);
        }
      }

      const myPro = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(111);
        }, 1000);
      });
      myPro.then().then(r => {
        console.log('r', r);
      });
    </script>
  </body>
</html>

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐