手寫系列 帶你實現乙個簡單的Promise

2022-10-08 16:48:29 字數 4434 閱讀 2941

學習之前 需要先對promise有個基本了解哦,這裡都預設大家都是比較熟悉promise的

本次將帶小夥伴們實現promise的基本功能

promise的基本骨架

promisethen

promise.then的多次呼叫

then鏈式呼叫

catch的實現

finally的實現

const promise_status_pending = "promise_status_pending";

const promise_status_fulfilled = "promise_status_fulfilled";

const promise_status_rejected = "promise_status_rejected";

class zxpromise

}const rejected = (reason) =>

}executor(resolve, rejected)

}}// 初步搭建好promise的construtor結構

const promise = new zxpromise((resolve, rejected) => )

const promise_status_pending = "promise_status_pending";

const promise_status_fulfilled = "promise_status_fulfilled";

const promise_status_rejected = "promise_status_rejected";

class zxpromise )

}} const rejected = (reason) => )

}} executor(resolve, rejected)

} then(onfufilled, onrejected)

}// 接下來開始寫then方法

const promise = new zxpromise((resolve, rejected) => )

promise.then((res) => , (err) => )

p1.then((res) => );

p1.then((res) => );

settimeout(() => )

}, 1000);

現在我們來解決下上述問題,看**

const promise_status_pending = "promise_status_pending";

const promise_status_fulfilled = "promise_status_fulfilled";

const promise_status_rejected = "promise_status_rejected";

class zxpromise );})}

} const rejected = (reason) => )})}

} executor(resolve, rejected)

} // 接下來為了promise能夠多次呼叫 進行優化

then(onfufilled, onrejected)

if (this.status === promise_status_rejected)

if (this.status === promise_status_pending)

}}

要想實現鏈式呼叫,那麼then方法肯定是將promise物件又給返回出來了,說到這了大家有沒有思路呢?

const promise_status_pending = "promise_status_pending";

const promise_status_fulfilled = "promise_status_fulfilled";

const promise_status_rejected = "promise_status_rejected";

class zxpromise );})}

} const rejected = (reason) => )})}

} trycatch(err)

}then(onfufilled, onrejected) catch (err)

}if (this.status === promise_status_rejected) catch (err)

}if (this.status === promise_status_pending) );

} catch (err)

try );

} catch (err)

}}) }}

const promise = new zxpromise((resolve, rejected) => )

promise.then((res) => , (err) => ).then((res) => , (err) => )

catch方法實際上是then第二個引數的語法糖,說到這裡大家有沒有明白什麼呢?

const promise_status_pending = "promise_status_pending";

const promise_status_fulfilled = "promise_status_fulfilled";

const promise_status_rejected = "promise_status_rejected";

const execfnwithcatcherror = (execfn, value, resolve, reject) => catch (err)

}class zxpromise );})}

}const rejected = (reason) => )

return this.reason;})}

}executor(resolve, rejected)

}then(onfufilled, onrejected)

onrejected = onrejected || defaultonrejected

return new zxpromise((resolve, rejected) =>

if (this.status === promise_status_rejected && onrejected)

if (this.status === promise_status_pending) );

if (onrejected) );}}

})}

catch(onrejected)

}

finally就是要在最後執行的函式,無論什麼情況,實現起來也是非常簡單

finally(fn) , () => );

}

const promise_status_pending = "promise_status_pending";

const promise_status_fulfilled = "promise_status_fulfilled";

const promise_status_rejected = "promise_status_rejected";

const execfnwithcatcherror = (execfn, value, resolve, reject) => catch (err)

}class zxpromise );})}

}const rejected = (reason) => )

return this.reason;})}

}executor(resolve, rejected)

}then(onfufilled, onrejected)

onrejected = onrejected || defaultonrejected

return new zxpromise((resolve, rejected) =>

if (this.status === promise_status_rejected && onrejected)

if (this.status === promise_status_pending) );

if (onrejected) );}}

})}

catch(onrejected)

finally(fn) , () => );

}}

手寫乙個簡單的HashMap

package com.lzq.mapinte ce public inte ce map package com.lzq.hashmap public class entry 編寫hashmap類 package com.lzq.hashmap import com.lzq.mapinte ce....

動手寫乙個簡單的promise

promise 是非同步程式設計的一種解決方案,比傳統的解決方案 函式和事件 更合理和更強大。它由社群最早提出和實現,es6 將其寫進了語言標準,統一了用法,原生提供了promise物件。所謂promise,簡單說就是乙個容器,裡面儲存著某個未來才會結束的事件 通常是乙個非同步操作 的結果。從語法上...

帶你實現乙個簡單實用的時間線

相信有點自定義基礎的哥們一看這個效果圖就覺得很簡單,還請各路大神不要吐槽哈。首先來分析一下這個效果圖,由三部分組成 1.下面那條預設的線 2.上面那條被選中的線 3.以及線下面的文字。繪製一條線,是自定義view中最基礎的操作了,就是呼叫canvas的drawline方法,隨便貼下 draw fir...