Advertisement

手写实现完整的Promise及其方法

阅读量:

信息整合来源于promise手写实现

复制代码
    const PENDING = 'PENDING';
    const FULFILLED = 'FULFILLED';
    const REJECTED = 'REJECTED';
    
    const resolvePromise = (promise2, x, resolve, reject) => {
      // Promise/A+ 2.3.3.3.3 只能调用一次
      let called;
      // 后续的条件要严格判断 保证代码能和别的库一起使用
      if (x instanceof Promise) { 
    try {
      // 为了判断 resolve 过的就不用再 reject 了(比如 reject 和 resolve 同时调用的时候)  Promise/A+ 2.3.3.1
      let then = x.then;
        // 不要写成 x.then,直接 then.call 就可以了 因为 x.then 会再次取值,Object.defineProperty  Promise/A+ 2.3.3.3
        then.call(x, y

全部评论 (0)

还没有任何评论哟~