标签:tsx keyword const padding var parameter 防止 循环引用 ble
class Promise{
constructor(excutor){
this.value = ‘‘;
this.reason = ‘‘;
this.status = ‘padding‘
this.onFulfilledCallback = []
this.onRejectedCallback = []
let resolve = (value)=>{
/*2.这个判断是为了status不可逆 只能从 padding转化为 成功或者失败*/
if (this.status == ‘padding‘) {
this.status = ‘fulfilled‘
this.value = value
/*3.当转态改变的时候依次执行队列里面储存的then函数里面对应的回调*/
this.onFulfilledCallback.forEach(fn=>{
fn()
})
}
};
let reject = (reason)=>{
/*2.这个判断是为了status不可逆 只能从 padding转化为 成功或者失败*/
if (this.status == ‘padding‘) {
this.status = ‘rejected‘
this.reason = reason
/*3.当转态改变的时候依次执行队列里面储存的then函数里面对应的回调*/
this.onRejectedCallback.forEach(fn=>{
fn()
})
}
};
/*1. 当发生异常是捕获异常 */
try{
excutor(resolve,reject)
}catch (e){
reject(e)
}
}
then(onFulfilled,onRejected){
//4.防止使用者不传成功或失败回调函数,所以成功失败回调都给了默认回调函数
onFulfilled = typeof onFulfilled === "function" ? onFulfilled : value => value;
onRejected = typeof onRejected === "function" ? onRejected : error => { throw error };
let newPromise;
if(this.status == ‘fulfilled‘){
return newPromise = new Promise((resolve,reject)=>{
setTimeout(()=>{
try{
let x = onFulfilled(this.value)
this.resolvePromise(newPromise, x, resolve, reject);
}catch (e){
reject(e)
}
})
})
}
if(this.status == ‘rejected‘){
return newPromise = new Promise((resolve,reject)=>{
setTimeout(()=>{
try{