手寫(xiě)Promise

基本實(shí)現(xiàn)new Promise 和 then /catch方法

  • Promise.js
class Promise {
  constructor(excutorCallback) {
    this.status = 'pending' //new Primise 時(shí)為進(jìn)行中
    this.value = undefined //resolve/reject的初始值(參數(shù))
    
    this.fulfilledAry = [] //成功之后要做的事
    this.rejectedAry = [] //失敗之后要做的事

    let resolveFn = result => {
      let timer = setTimeout(() => {
        clearTimeout(timer)  
        if (this.status !== 'pending') return
        this.status = 'fulfilled'
        this.value = result

        this.fulfilledAry.forEach(item => {
          item(this.value)
        })
        }, 0)
    }

    let rejectFn = reason => {
      let timer = setTimeout(() => {
        clearTimeout(timer)
        if (this.status !== 'pending') return
        this.status = 'rejected'
        this.value = reason

        this.rejectedAry.forEach(item => {
          item(this.value)
        })
      }, 0)
    }

    //實(shí)例化時(shí)異常捕獲
    try {
      excutorCallback(resolveFn, rejectFn)
    } catch (err) {
      //異常按rejected狀態(tài)處理
      rejectFn(err)
    }
  }

  //原型上加方法
  then (fulfilledCallback, rejectedCallback) {
    //then的參數(shù)不傳的情況 給它一個(gè)函數(shù)以承接數(shù)據(jù) 在后面的then里面就可以拿到了
    typeof fulfilledCallback !== 'function' ? fulfilledCallback = result => result : null

    typeof rejectedCallback !== 'function' ? rejectedCallback = reason => {
      throw new Error(reason.message)
    } : null

    //鏈?zhǔn)讲僮鞣祷匾粋€(gè)新的Promise實(shí)例
    return new Promise((resolve, reject) => {
      this.fulfilledAry.push(() => {
        try {
          let x = fulfilledCallback(this.value)
          x instanceof Promise ? x.then(resolve, reject) : resolve(x)
        } catch (err) {
          reject(err)
        }
        
      })
      this.rejectedAry.push(() => {

        try {
          let x = rejectedCallback(this.value)
          x instanceof Promise ? x.then(resolve, reject) : resolve(x)
        } catch (err) {
          reject(err)
        }
      })
    })
  }

  //原型上添加catch方法
  catch (rejectedCallback) {
    return this.then(null, rejectedCallback)
  }
}

module.exports = Promise
  • test.js測(cè)試 調(diào)用手寫(xiě)的Promise函數(shù)
const Promise = require('./Promise')

new Promise((resolve, reject) => {

  // throw new Error('wrong')
  resolve(100)

  reject(-100)

}).then((result) => {
  console.log(result);
  console.log('成功')
  
}, (reason) => {
  console.log(reason)
  console.log('失敗')

})

console.log('同步方法');
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Promise的含義 Promise在JavaScript語(yǔ)言中早有實(shí)現(xiàn),ES6將其寫(xiě)進(jìn)了語(yǔ)言標(biāo)準(zhǔn),統(tǒng)一了用法...
    Alex灌湯貓閱讀 887評(píng)論 0 2
  • 官方中文版原文鏈接 感謝社區(qū)中各位的大力支持,譯者再次奉上一點(diǎn)點(diǎn)福利:阿里云產(chǎn)品券,享受所有官網(wǎng)優(yōu)惠,并抽取幸運(yùn)大...
    HetfieldJoe閱讀 8,771評(píng)論 0 29
  • 官方中文版原文鏈接 感謝社區(qū)中各位的大力支持,譯者再次奉上一點(diǎn)點(diǎn)福利:阿里云產(chǎn)品券,享受所有官網(wǎng)優(yōu)惠,并抽取幸運(yùn)大...
    HetfieldJoe閱讀 11,111評(píng)論 26 95
  • Promise 對(duì)象 Promise 的含義 Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函...
    neromous閱讀 8,823評(píng)論 1 56
  • 花 賦詩(shī) 韻茶 香蕾破 胭脂灑 暗香沾襟 ...
    珊珊Y閱讀 190評(píng)論 0 1

友情鏈接更多精彩內(nèi)容