当前位置:网站首页>Handwriting promise
Handwriting promise
2022-07-21 20:37:00 【Lotus awn】
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
function Promise(executor) {
var _this = this
this.state = PENDING; // state
this.value = undefined; // Successful results
this.reason = undefined; // Reasons for failure
this.onFulfilled = [];// Successful callback
this.onRejected = []; // Failed callback
function resolve(value) {
if(_this.state === PENDING){
_this.state = FULFILLED
_this.value = value
_this.onFulfilled.forEach(fn => fn(value))
}
}
function reject(reason) {
if(_this.state === PENDING){
_this.state = REJECTED
_this.reason = reason
_this.onRejected.forEach(fn => fn(reason))
}
}
try {
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
Promise.prototype.then = function (onFulfilled, onRejected) {
if(this.state === FULFILLED){
typeof onFulfilled === 'function' && onFulfilled(this.value)
}
if(this.state === REJECTED){
typeof onRejected === 'function' && onRejected(this.reason)
}
if(this.state === PENDING){
typeof onFulfilled === 'function' && this.onFulfilled.push(onFulfilled)
typeof onRejected === 'function' && this.onRejected.push(onRejected)
}
}
边栏推荐
- window. history. back(); The problem that layer cannot be closed after returning
- 扫雷(C语言)
- 受控组件和非受控组件的区别
- 第四局 借问酒家何处有?牧童遥指杏花村 下
- Self taught programming for more than 80 years, these private practical tools & learning websites have accompanied me to the present. I must collect them and double my learning efficiency - Website
- LaTeX:导数相关符号
- Data analysis on the fourth match
- 求最小公倍数
- 格式化时间
- 使用tailwind在谷歌浏览器上,button会出现蓝色背景的问题
猜你喜欢
随机推荐
C. Binary string (prefix sum)
Read the manuscript in pieces
为什么用快慢指针找链表的环,快指针和慢指针一定会相遇?
Read the manuscript in pieces
22.【【静态成员访问格式】类名::访问成员,则该成员需要是静态】
Analysis of matching data under the fourth game
12.【I/O流get()与getline()与put()函数】
boost::this_thread::sleep (boost::posix_time::microseconds (100000))报错“this_thread”:“::”左侧的符号必须是一种类型
The fifth Bureau, akali teaching Bureau
(剑指offer版)字符串逆置
codeforces 1647A Madoka and Math Dad
开发模式下热更新 加快开发环境代码更新
xml建模
[递归]阿克曼函数
useMemo和useCallback的区别以及使用场景
格式化时间
image 多图片页面的优化方式
2020常州市程序设计小能手真题及题解
碎碎念 碎碎念 起稿
增删改查~