实现一个promise

先看一下promise的基本用法

const promise = new Promise((resolve, reject)=>{
    resolve('data')
  })

  promise.then((res)=>{
    console.log(res)
  }, (err)=>{
    console.log(err)
  })

初步实现

由此可以看出
1、promise函数接受一个参数executor,函数executor接受两个入参,分别是resolve,reject;
2、promise实例有一个then方法,then方法接受两个参数,分别是函数onfulfilled,onrejected;

function Promise (executor) {
  // 此处使用箭头函数是要将resolve内的this绑定在promise实例上
  const resolve = value => {
  }
  const reject = reason => {
  }
  executor(resolve, reject)
}
Promise.prototype.then = function (onfulfilled, onrejected) {
  //此处为onfulfilled、onrejected 设一个默认值
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
}

当resolve执行的时候,会获取到一个值value,该值会传递给onfulfilled,所以会有一个value的变量存储resolve传进来的值,同理有一个变量reason存储reject传进来的值

function Promise (executor) {
  this.value = null
  this.reason = null

  // 此处使用箭头函数是要将resolve内的this绑定在promise实例上
  const resolve = value => {
    this.value = value
  }

  const reject = reason => {
    this.reason = reason
  }

  executor(resolve, reject)
}
Promise.prototype.then = function (onfulfilled, onrejected) {
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}

  onfulfilled(this.value)
  onrejected(this.reason)
}

状态判断

该方法已经可以实现最开始的那个例子,但是有一个问题,如果此时我们同同时执行resolev, reject时,then方法中也会同时执行onfulfilled,onrejected;

const promise = new Promise((resolve, reject)=>{
  resolve('data')
  reject('error')
})

promise.then((res)=>{
  console.log(res)
}, (err)=>{
  console.log(err)
})

所以此处需要增加一个状态判断,当状态status由pending变为fulfilled后,onrejected就不再执行,实现如下:

function Promise (executor) {
  this.value = null
  this.reason = null
  this.status = 'pending'

  // 此处使用箭头函数是要将resolve内的this绑定在promise实例上
  const resolve = value => {
    if (this.status === 'pending') {
      this.value = value
      this.status = 'fulfilled'
    }
  }

  const reject = reason => {
    if (this.status === 'pending') {
      this.reason = reason
      this.status = 'rejected'
    }
  }

  executor(resolve, reject)
}
Promise.prototype.then = function (onfulfilled, onrejected) {
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
  if (this.status === 'fulfilled') {
    onfulfilled(this.value)
  }
  if (this.status === 'rejected') {
    onrejected(this.reason)
  }
}

异步实现

该版本只能执行同步代码,异步代码却不起作用

const promise = new Promise((resolve, reject)=>{
    setTimeout(()=>{
      resolve('data')
    },100)
})

promise.then((res)=>{
  console.log(res)
}, (err)=>{
  console.log(err)
})

这段代码执行的时候,控制台没有任何打印,promise是用来处理异步任务的,所以我们要将Promise改进一下,那么如何改进呢?
改变then方法内onfulfilled、onrejected的执行时机,不能在then方法内立即执行,而是要等到resove执行的时候再执行,所以可以将onfulfilled、onrejected挂载到promise实例上,在resove中执行,实现如下:

function Promise (executor) {
  this.value = null
  this.reason = null
  this.status = 'pending'
  this.onfulfilled = Function.prototype
  this.onrejected = Function.prototype

  // 此处使用箭头函数是要将resolve内的this绑定在promise实例上
  const resolve = value => {
    if (this.status === 'pending') {
      this.value = value
      this.status = 'fulfilled'
      this.onfulfilled(this.value)
    }
  }

  const reject = reason => {
    if (this.status === 'pending') {
      this.reason = reason
      this.status = 'rejected'
      this.onrejected(this.reason)
    }
  }

  executor(resolve, reject)
}
Promise.prototype.then = function (onfulfilled, onrejected) {
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
  if (this.status === 'fulfilled') {
    onfulfilled(this.value)
  }
  if (this.status === 'rejected') {
    onrejected(this.reason)
  }

  if (this.status === 'pending') {
    this.onfulfilled = onfulfilled
    this.onrejected = onrejected
  }
}

下面看一下这个例子输出结果

const promise = new Promise((resolve, reject)=>{
    resolve('data')
})

promise.then((res)=>{
  console.log(res)
})

console.log(1)

上面代码中,先打印'data',后打印1;这是错误的,promise执行时机要在同步任务执行完毕后,再执行promise的代码,所以我们要对代码进行改造,使其先输出1,后输出‘data’

function Promise (executor) {
  this.value = null
  this.reason = null
  this.status = 'pending'
  this.onfulfilled = Function.prototype
  this.onrejected = Function.prototype

  // 此处使用箭头函数是要将resolve内的this绑定在promise实例上
  const resolve = value => {
    if (this.status === 'pending') {
      if (value instanceof Promise) {
        return value.then(resolve, reject)
      }
      setTimeout(() => {
        this.value = value
        this.status = 'fulfilled'
        this.onfulfilled(this.value)
      })
    }
  }

  const reject = reason => {
    if (this.status === 'pending') {
      setTimeout(() => {
        this.reason = reason
        this.status = 'rejected'
        this.onrejected(this.reason)
      })
    }
  }

  executor(resolve, reject)
}
Promise.prototype.then = function (onfulfilled, onrejected) {
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
  if (this.status === 'fulfilled') {
    onfulfilled(this.value)
  }
  if (this.status === 'rejected') {
    onrejected(this.reason)
  }

  if (this.status === 'pending') {
    this.onfulfilled = onfulfilled
    this.onrejected = onrejected
  }
}

使用setTimeout使其在同步代码执行完成后执行;下面再看一个例子

const promise = new Promise((resolve, reject)=>{
    resolve('data')
})
promise.then((res)=>{
  console.log(res)
})

promise.then((res)=>{
  console.log(res)
})

此时只打印了一次‘data’,理论上应该打印两次data;出现该问题的原因是resolve在执行的时候,只执行了一次onfulfilled,此时可能会有多个onfulfilled待执行,所有挂载在实例上的onfulfilled应该是一个数组,resove的时候,循环执行数组内的所有函数

function Promise (executor) {
  this.value = null
  this.reason = null
  this.status = 'pending'
  this.onfulfilledArry = []
  this.onrejectedArry = []

  // 此处使用箭头函数是要将resolve内的this绑定在promise实例上
  const resolve = value => {
    if (this.status === 'pending') {
      if (value instanceof Promise) {
        return value.then(resolve, reject)
      }
      setTimeout(() => {
        this.value = value
        this.status = 'fulfilled'
        this.onfulfilledArry.forEach(func => func(this.value))
      })
    }
  }

  const reject = reason => {
    if (this.status === 'pending') {
      setTimeout(() => {
        this.reason = reason
        this.status = 'rejected'
        this.onrejectedArry.forEach(func => func(this.reason))
      })
    }
  }

  try {
    executor(resolve, reject)
   } catch (e) {
     reject(e)
   }
}
Promise.prototype.then = function (onfulfilled, onrejected) {
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
  if (this.status === 'fulfilled') {
    onfulfilled(this.value)
  }
  if (this.status === 'rejected') {
    onrejected(this.reason)
  }

  if (this.status === 'pending') {
    this.onfulfilledArry.push(onfulfilled)
    this.onrejectedArry.push(onrejected)
  }
}

上面代码中已经初步实现了一个promise,增加了错误捕获,捕获到的错误直接执行reject

promise的链式调用

promise的then会返回一个新的promise2,并将当前onfulfilled的执行结果作为参数传递给promise2的resolve,像下面这样

        Promise.prototype.then = function (onfulfilled, onrejected) {
          onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
          onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
          let promise2 
          if (this.status === 'fulfilled') {
            return promise2 = new Promise((resolve, reject)=>{
                setTimeout(()=>{
                    try {
                        let result = onfulfilled(this.value)
                        resolve(result)
                    } catch (e) {
                        reject(e)
                    }
                    
                })
            })
          }
          if (this.status === 'rejected') {
            onrejected(this.reason)
          }

          if (this.status === 'pending') {
            this.onfulfilledArry.push(onfulfilled)
            this.onrejectedArry.push(onrejected)
          }
        }

同理,status为rejected、pending也做相同操作,pending时,推入数组内的是一个函数,该函数执行时,执行promise2的resolve,then方法补充后,代码如下:

Promise.prototype.then = function (onfulfilled, onrejected) {
  onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
  onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
  let promise2 
  if (this.status === 'fulfilled') {
    return promise2 = new Promise((resolve, reject)=>{
        setTimeout(()=>{
            try {
                let result = onfulfilled(this.value)
                resolve(result)
            } catch (e) {
                reject(e)
            }
            
        })
    })
  }
  if (this.status === 'rejected') {
    return promise2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            try {
                let result = onrejected(this.reason)
                resolve(result)
            } catch(e) {
                reject(e)
            }
        })
    })
  }

  if (this.status === 'pending') {
    return promise2 = new Promise((resolve, reject) => {
        // 此时onfulfilled函数不会立即执行,所以不需要使用setTimeout做异步处理
        this.onfulfilledArry.push(value => {
            try {
                let result = onfulfilled(value)
                resolve(result)
            } catch (e) {
                reject(e)
            }
        })

        this.onrejectedArry.push(reason => {
            try {
                let result = onrejected(reason)
                resolve(result)
            } catch (e) {
                reject(e)
            }
        })
    })
  }
}

此时运行下面代码,可正常执行,打印:99data

const promise = new Promise((resolve, reject)=>{
    resolve('data')
})

promise.then((res)=>{
  return new Promise((resolve, reject) => {
    setTimeout(()=>{
        resolve('99' + res)
    }, 2000)
  }) 
}).then((res)=>{
  console.log(res)
})

此时promsie链式调用已基本完成,如果我们的第一个then方法中return的不是我们自己定义的Promise实例,而是官方定义的Promise实例,这个时候仅仅使用resolve(result)是处理不了很多种情况的,这个时候需要抽离出一个resolvePromise方法,将返回的promise2, result , promise2的resolve,promise2的reject作为参数传进去,在resolvePromise统一处理;类似这样

....
            try {
                let result = onfulfilled(value)
                 resolvePromise(promise2, data, resolve, reject)
            } catch (e) {
                reject(e)
            }
....

实现resolvePromise的完整方法如下:

const resolvePromise = (promise2, result, resolve, reject) => {
    // 当result与promise2相同时
    if (result === promise2) {
      reject(new TypeError('error circular'))
      return
    }

    // 是否已经执行过onfulfilled || onrejected
    let consumed = false
    let thenabel
    if (result instanceof Promise) {
      if (result.status === 'pending') {
        result.then(function (data) {
          resolvePromise(promise2, data, resolve, reject)
        }, reject)
      } else {
        result.then(resolve, reject)
      }
      return 
    }
    // 此时处理疑似promise的情况
    let isComplexResult = target => (typeof target === 'function' || typeof target === 'object') && target !== null
    if(isComplexResult(result)) {
      try {
        thenabel = result.then
        // 判断是否是promise类型
        if (typeof thenabel === 'function') {
          thenabel.call(result, function (data) {
            if (consumed) {
              return
            }
            consumed = true
            return resolvePromise(promise2, data, resolve, reject)
          }, function (error) {
            if (consumed) {
              return
            }
            consumed = true
            return reject(result)
          })
        }

      } catch (e) {
        if (consumed) {
          return
        } 
        consumed = true
        return reject(e)
      }
    } else {
      resolve(result)
    }
  }

到这里,promise已经基本完成,补充几个静态方法后,完整代码如下:

function Promise (executor) {
    this.status = 'pending'
    this.value = null
    this.reason = null
    this.onfulfilledArry = []
    this.onrejectedArry = []

    const resolve = value => {

      // 如果传递进来的是一个promise类型
      if (value instanceof Promise) {
        return value.then(resolve, reject)
      }
      setTimeout(() => {
        if (this.status === 'pending') {
          this.value = value
          this.status = 'fulfilled'
          this.onfulfilledArry.forEach(item => item(this.value))
        }
      })
    }

    const reject = reason => {
      setTimeout(() => {
        if (this.status === 'pending') {
          this.reason = reason
          this.status = 'rejected'
          this.onrejectedArry.forEach(item => item(this.reason))
        }
      })
    }
    try {
      executor(resolve, reject)
    } catch (e) {
      reject(e)
    }
  }
  const resolvePromise = (promise2, result, resolve, reject) => {
    // 当result与promise2相同时
    if (result === promise2) {
      reject(new TypeError('error circular'))
      return
    }

    // 是否已经执行过onfulfilled || onrejected
    let consumed = false
    let thenabel
    if (result instanceof Promise) {
      if (result.status === 'pending') {
        result.then(function (data) {
          resolvePromise(promise2, data, resolve, reject)
        }, reject)
      } else {
        result.then(resolve, reject)
      }
      return 
    }
    // 此时处理疑似promise的情况
    let isComplexResult = target => (typeof target === 'function' || typeof target === 'object') && target !== null
    if(isComplexResult(result)) {
      try {
        thenabel = result.then
        // 判断是否是promise类型
        if (typeof thenabel === 'function') {
          thenabel.call(result, function (data) {
            if (consumed) {
              return
            }
            consumed = true
            return resolvePromise(promise2, data, resolve, reject)
          }, function (error) {
            if (consumed) {
              return
            }
            consumed = true
            return reject(result)
          })
        }

      } catch (e) {
        if (consumed) {
          return
        } 
        consumed = true
        return reject(e)
      }
    } else {
      resolve(result)
    }
  }

  Promise.prototype.then = function (onfulfilled, onrejected) {

    // promise穿透实现   promsie.then(null).then(res=>res)
    onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
    onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
    let promise2
    if (this.status === 'fulfilled') {
      return  promise2 = new Promsie((resolve, reject) => {
        setTimeout(() => {
          try {
            let result = onfulfilled(this.value)
            resolvePromise(promise2, result, resolve, reject)
          } catch(e) {
            reject(e)
          }
        })
      })
    }

    if (this.status === 'rejected') {
      return promise2 = new Promise((resolve, reject) => {
        setTimeout(() => {
          try {
            let result = onrejected(this.reason)
            resolvePromise(promise2, result, resolve, reject)
          } catch (e) {
            reject(e)
          }
        })
      })
    }  

    if (this.status === 'pending') {
      return promise2 = new Promise((resolve, reject) => {
        this.onfulfilledArry.push(value => {
          try {
            let result = onfulfilled(value)
            resolvePromise(promise2, result, resolve, reject)
          } catch (e) {
            reject(e)
          }
        })
        
        this.onrejectedArry.push(reason => {
          try {
            let result = onrejected(reason)
            resolvePromise(promise2, result, resolve, reject)
          }catch (e) {
            reject(e)
          }
        })
          
      })
    }
  }
  // catch方法
  Promise.prototype.catch = function (catchFunc) {
    return this.then(null, catchFunc)
  }
  // resove方法
  Promise.resolve = function (value) {
    return new Promise((resolve, reject)=>{
      resolve(value)
    }) 
  }

  // reject方法
  Promise.reject = function (reason) {
    return new Promise ((resolve, reject)=>{
      reject(value)
    }) 
  }
  
  // all方法
  Promise.all = function (promiseArry) {
    if (!Array.isArray(promiseArry)) {
      throw new TypeError('should array')
    }
    return new Promsie((resolve, reject) => {
      try {
        let resultArry = []
        const length = promiseArry.length
        for (let i=0; i<length; i++) {
          promiseArry[i].then(data=>{
            resultArry.push(data)
            if (resultArry.length === length) {
              resolve(resultArry)
            }
          }, reject)
        }
      } catch (e) {
        reject(e)
      }
    })
  }
  // race方法
  Promise.race = function (promiseArry) {
    if (!Array.isArray(promiseArry)) {
      throw new TypeError('should array')
    }
    return new Promise((resolve, reject) => {
      try {
        const length = promiseArry.length;
        for (let i=0; i<length; i++) {
          promiseArry[i].then(resolve, reject)
        }
      } catch (e) {
        reject(e)
      }
    })
  }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,980评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,178评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,868评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,498评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,492评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,521评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,910评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,569评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,793评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,559评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,639评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,342评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,931评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,904评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,144评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,833评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,350评论 2 342

推荐阅读更多精彩内容