深入理解promise底层源码有助于我们更好地理解promise。以下代码可以实现promise的基本功能。
class Promise{
// 构造方法
constructor(executor){
// 添加属性
this.PromiseState = "pending";
this.PromiseResult = null;
this.callbacks = [];
// 保存实例对象this的值
const self = this; //self _this that
// resolve函数
function resolve(data) {
// 判断状态
if (self.PromiseState !== "pending") return;
// 1.修改对象的状态(promiseState)
self.PromiseState = "fulfilled"; //resolved
// 2.设置对象结果值(PromiseResult)
self.PromiseResult = data;
// 调用成功的回调函数(定时器实现异步回调)
setTimeout(()=>{
self.callbacks.forEach((item) => {
item.onResolved(data);
});
})
}
// reject函数
function reject(data) {
if (self.PromiseState !== "pending") return;
// 1.修改对象的状态(promiseState)
self.PromiseState = "rejected"; //resolved
// 2.设置对象结果值(PromiseResult)
self.PromiseResult = data;
//执行失败的回调函数(定时器实现异步回调)
setTimeout(()=>{
self.callbacks.forEach((item) => {
item.onRejected(data);
});
})
}
try {
// 同步调用执行器函数
executor(resolve, reject);
} catch (e) {
reject(e);
}
}
// then方法
then(onResolved,onRejected){
const self = this;
// 判断回调函数参数
if(typeof onRejected !=='function'){
onRejected=reason=>{
throw reason
}
}
if(typeof onResolved !=='function'){
onResolved=value=>value
}
return new Promise((resolve, reject) => {
// 封装函数
function callback(type){
try {
//获得回调函数的执行结果
let result = type(self.PromiseResult);
// 判断
if (result instanceof Promise) {
//如果是Promise类型的对象
result.then(
(v) => {
resolve(v);
},
(r) => {
reject(r);
}
);
} else {
//结果的对象状态为成功
resolve(result);
}
} catch (e) {
reject(e);
}
}
// 调用回调函数 PromiseState
if (this.PromiseState === "fulfilled") {
// (定时器实现异步回调)
setTimeout(()=>{
callback(onResolved)
})
}
if (this.PromiseState === "rejected") {
// (定时器实现异步回调)
setTimeout(()=>{
callback(onRejected)
})
}
// 判断pending状态
if (this.PromiseState === "pending") {
// 保存回调函数
this.callbacks.push({
onResolved: function () {
callback(onResolved)
},
onRejected: function () {
callback(onRejected)
},
});
}
});
}
// catch方法
catch(onRejected){
return this.then(undefined,onRejected)
}
// resolve方法
static resolve(value){
return new Promise((resolve,reject)=>{
if(value instanceof Promise){
value.then(v=>{
resolve(v)
},r=>{
reject(r)
})
}
else{
resolve(value)
}
})
}
// reject方法
static reject(reason){
return new Promise((resolve,reject)=>{
reject(reason)
})
}
// all方法
static all(promises){
// 返回结果为promise对象
return new Promise((resolve,reject)=>{
// 声明变量
let count=0;
let arr=[]
// 遍历
for(let i=0;i<promises.length;i++){
promises[i].then(v=>{
// 得知对象的状态为成功
// 每个promise对象都成功
count++;
// 将当前的promise对象成功的结果 存入到数组中
arr[i]=v
if(count===promises.length){
// 修改状态
resolve(arr)
}
},r=>{
reject(r)
})
}
})
}
// race方法
static race(promises){
return new Promise((resolve,reject)=>{
for(let i=0;i<promises.length;i++){
promises[i].then(v=>{
resolve(v)
},r=>{
reject(r)
})
}
})
}
}