手写Promise
这里写目录标题
- 手写Promise
- 手写Promise的规范手册promisesaplus官网
- 手写Promise-结构的设计
手写Promise的规范手册promisesaplus官网
链接: 官网链接
手写Promise-结构的设计
// 手写Promsie
const PROMISE_STATUS_PENDING = 'pending' //等待状态
const PROMISE_STATUS_FULFILLED = 'fulfilled' //完成状态
const PROMISE_STATUS_REJECTED = 'rejected' //拒绝/失败状态
class HyPromise{
constructor(excutor){
this.statu = PROMISE_STATUS_PENDING
this.value = undefined
this.reason = undefined
const resolve = (value) => {
if(this.statu === PROMISE_STATUS_PENDING){
this.statu = PROMISE_STATUS_FULFILLED
this.value = value
console.log('resolve函数调用了');
}
}
const reject = (reason) => {
if(this.statu === PROMISE_STATUS_PENDING){
this.statu = PROMISE_STATUS_REJECTED
this.reason = reason
console.log('reject函数调用了');
}
}
excutor(resolve,reject)
}
}
const promise = new HyPromise((resolve,reject) => {
console.log('执行者函数调用');
reject('1111111');
resolve();
})
promise.then(res => {
}).catch(err => {
})