手寫promise(then鏈的執(zhí)行原理)(2)

class Promise{

????constructor(excutorCallBack){

????????this.status = 'pending'

????????this.value = undefined;

????????this.fulfilledAry = []; //管控,必須得then執(zhí)行后才能執(zhí)行resolveFn方法,成功要做的方法

????????this.rejectedAry = []; //失敗要做的方法


????????// => 執(zhí)行EXCUTOP

????????let resolveFn = (result) =>{

????????????let timer = setTimeout(()=>{

????????????????if(this.status !== 'pending') return;

????????????????clearTimeout(timer)

????????????????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)

????????}

????????// => 執(zhí)行EXCUTOP(異常捕獲)

????????try{

????????????excutorCallBack(resolveFn,rejectFn);

????????} catch (err){

????????????// 有異常信息按照rejected狀態(tài)信息處理

????????????rejectFn(err);

????????}

????}

????then (fulfilledCallBack,rejectedCallBack) {

????????// fulfilledCallBack不傳遞的情況

????????typeof fulfilledCallBack !== 'function'? fulfilledCallBack = result =>{

????????????return result

????????} : null;

????????typeof rejectedCallBack !== 'function'? rejectedCallBack = reason =>{

????????????throw new Error(reason.message)

????????} : null

????????// 返回一個新的Promise實例

????????return new Promise((resolve,reject)=>{

????????????this.fulfilledAry.push(()=>{

????????????????try {

????????????????????let x = fulfilledCallBack(this.value);

????????????????????if(x instanceof Promise){

????????????????????????x.then(resolve,reject);

????????????????????????return;

????????????????????}

????????????????????resolve(x);

????????????????}catch(err){

????????????????????reject(err)

????????????????}

????????????});

????????????this.rejectedAry.push(()=>{

????????????????try{

????????????????????let x = rejectedCallBack(this.value);

????????????????????if(x instanceof Promise){

????????????????????????x.then(resolve,reject);

????????????????????????return;

????????????????????}

????????????????????resolve(x)

????????????????}catch(err){

????????????????????reject(x)

????????????????}

????????????})

????????});

????}

????catch(rejectedCallBack){

????????return this.then(null,rejectedCallBack)

????}

}

module.exports = Promise;

then鏈的執(zhí)行原理
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 前言 本文旨在簡單講解一下javascript中的Promise對象的概念,特性與簡單的使用方法。并在文末會附上一...
    _暮雨清秋_閱讀 2,315評論 0 3
  • 在ES6當(dāng)中添加了很多新的API其中很值得一提的當(dāng)然少不了Promise,因為Promise的出現(xiàn),很輕松的就給開...
    嘿_那個誰閱讀 3,749評論 2 3
  • Promise 對象 Promise 的含義 Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函...
    neromous閱讀 8,835評論 1 56
  • 一、Promise的含義 Promise在JavaScript語言中早有實現(xiàn),ES6將其寫進(jìn)了語言標(biāo)準(zhǔn),統(tǒng)一了用法...
    Alex灌湯貓閱讀 888評論 0 2
  • ### 背景 Promise是異步編程的一種解決方案,它可以解決異步回調(diào)地獄的問題,防止層層嵌套對程序代碼帶來的難...
    小武song閱讀 1,239評論 1 0

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