废话不多说直接上代码,这里用到了 单例模式和 立即执行函数思想
const MyPromise = (() => {
// 定义三种状态
const PENDING = "pending";
const RESOLVED = "resolved";
const REJECTED = "rejected";
//定义不让外界访问的符号属性,状态和数据
const PromiseResult = Symbol("PromiseResult")
const PromiseState = Symbol("PromiseState")
const changeStatus = Symbol("changeStatus")
// 定义两个任务队列:成功的后续处理和失败后续处理
const thenables = Symbol("thenables")
const catchables = Symbol("catchables")
const settleHandle = Symbol("settleHandle")
return class MyPromise {
/*
newStatus 新的状态
newValue 新的值
queue 执行的任务队列
*/
[changeStatus](newStatus, newValue, queue) {
if (this[PromiseState] !== PENDING) {
return
}
this[PromiseState] = newStatus;
this[PromiseResult] = newValue;
// console.log(queue)
queue.forEach(element => {
// console.log(element)
return element(newValue)
});
}
constructor(executor) {
// 实例化对象的初始化
this[PromiseState] = PENDING;
this[PromiseResult] = undefined;
// 定义任务队列的数组
this[thenables] = []
this[catchables] = []
// 定义成功或失败的推向函数
const resolve = data => {
// if(this[PromiseState] !== PENDING){
// return
// }
// this[PromiseState] = RESOLVED;
// this[PromiseResult] = data;
this[changeStatus](RESOLVED, data, this[thenables])
}
const reject = reason => {
// if(this[PromiseState] !== PENDING){
// return
// }
// this[PromiseState] = REJECTED;
// this[PromiseResult] = reason;
this[changeStatus](REJECTED, reason, this[catchables])
}
// executor(resolve,reject)
try {
executor(resolve, reject)
} catch (error) {
reject(error)
}
}
then(thenable, catchable) {
// if(this[PromiseState] === RESOLVED){
// thenable(this[PromiseResult])
// }else{
// this[thenables].push(thenable)
// }
this[settleHandle](thenable, RESOLVED, this[thenables])
this.catch(catchable)
}
catch (catchable) {
// if(this[PromiseState] === REJECTED){
// catchable(this[PromiseResult])
// }else{
// this[catchables].push(catchable)
// }
this[settleHandle](catchable, REJECTED, this[catchables])
}
// 提取公共代码
/*
handler 后续处理函数
immediateStatus 立即执行的状态
queue 任务队列
*/
[settleHandle](handler, immediateStatus, queue) {
if (this[PromiseState] === immediateStatus) {
setTimeout(() => {
handler(this[PromiseResult])
}, 0)
} else {
queue.push(handler)
}
}
static resolve(data) {
if (data instanceof MyPromise) {
return data
} else {
return new MyPromise(res => {
res(data)
})
}
}
static reject(resaon) {
return new MyPromise((res, rej) => {
rej(resaon)
})
}
static all(proms) {
return new MyPromise((resolve, reject) => {
const results = proms.map(p => {
const obj = {
res: undefined,
isResolved: false
}
p.then(data => {
obj.res = data;
obj.isResolved = true;
const unResolved = results.filter(r => !r.isResolved)
if (unResolved.length === 0) {
// 全部都是resolve
resolve(results.map(r => r.res))
}
}, reson => {
reject(reson)
})
return obj;
})
})
}
static race(proms) {
return new MyPromise((resolve, reject) => {
proms.forEach(p =>{
p.then(data=>{
console.log(11)
resolve(data)
},err=>{
console.log(22)
reject(err)
})
})
})
}
}
})()
如果有什么问题,欢迎回访