时间:2020-11-18 来源:未知 分类:前端笔记 阅读:
const PENDING = 'pending';// 等待
const FULFILLED = 'fulfilled';// 等待
const REJECT = 'reject';// 等待
class MyPromise {
constructor(executor) {
try {
executor(this.resolve, this.reject)
} catch (e) {
console.log(e, e)
this.reject(e);
}
}
status = PENDING;// promise状态
value = undefined;// 成功之后的值
reason = undefined;// 失败后的原因
successCallback = [];// 成功回调
failCallback = [];// 失败回调
resolve = (value) => {
// 如果状态不是等待 阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改为成功
this.status = FULFILLED;
// 保存成功之后的值
this.value = value;
// 判断成功回调是否存在,如果存在就调用
// this.successCallback && this.successCallback(this.value);
while (this.successCallback.length) this.successCallback.shift()()
}
reject = (reason) => {
// 如果状态不是等待 阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改为失败
this.status = REJECT;
// 保存失败之后的原因
this.reason = reason;
// 判断失败回调是否存在,如果存在就调用
// this.failCallback && this.failCallback(this.reason);
while (this.failCallback.length) this.failCallback.shift()()
}
then(successCallback, failCallback) {
successCallback = successCallback ? successCallback : value => value;
failCallback = failCallback ? failCallback : reason => reason;
let promise2 = new MyPromise((resolve, reject) => {
// 判断状态
if (this.status === FULFILLED) {
setTimeout(() => {
try {
let x = successCallback(this.value);
// 判断x的值事普通值还是promise对象,如果是普通之,直接调用resolve
// 如果是promise对象,查看promiase返回的结果
// 再根据promise对象返回的结果决定调用resolve还是调用reject
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0);
} else if (this.status === REJECT) {
setTimeout(() => {
try {
let x = failCallback(this.value);
// 判断x的值事普通值还是promise对象,如果是普通之,直接调用resolve
// 如果是promise对象,查看promiase返回的结果
// 再根据promise对象返回的结果决定调用resolve还是调用reject
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0);
} else {
// 等待
//将成功回调和失败回调存储起来
this.successCallback.push(() => {
setTimeout(() => {
try {
let x = successCallback(this.value);
// 判断x的值事普通值还是promise对象,如果是普通之,直接调用resolve
// 如果是promise对象,查看promiase返回的结果
// 再根据promise对象返回的结果决定调用resolve还是调用reject
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0);
});
this.failCallback.push(() => {
setTimeout(() => {
try {
let x = failCallback(this.value);
// 判断x的值事普通值还是promise对象,如果是普通之,直接调用resolve
// 如果是promise对象,查看promiase返回的结果
// 再根据promise对象返回的结果决定调用resolve还是调用reject
resolvePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0);
});
}
});
return promise2
}
finally(callback) {
return this.then((value) => {
return MyPromise.resolve(callback()).then(() => value);
}, (reason) => {
return MyPromise.resolve(callback()).then(() => value);
throw reason;
})
}
catch(failCallback) {
return this.then(undefined, failCallback)
}
static all(array) {
const result = [];
let index = 0;
return new MyPromise((resolve, reject) => {
const addData = (key, value) => {
result[key] = value;
index++;
if (index === array.length) {
resolve(result);
}
}
for (let i = 0; i < array.length; i++) {
let current = array[i];
if (current instanceof MyPromise) {
// promise对象
current.then(value => addData(i, value), reason => reject(reason))
} else {
// 普通值
addData(i, array[i])
}
}
})
}
static resolve(value) {
if (value instanceof MyPromise) return value;
return new MyPromise(resolve => resolve(value));
}
}
const resolvePromise = (promise2, x, resolve, reject) => {
if (promise2 === x) {
return reject('TypeError: Chaining cycle detected for promise #<Promise>')
}
if (x instanceof MyPromise) {
// promise对象
x.then(resolve, reject)
} else {
// 普通值
resolve(x)
}
}
module.exports = MyPromise
橙子,热爱前端,关注前端,4年的前端工作经验,熟练掌握前端各项技能,熟练多种前端框架,希望遇到志同道合的前端朋友们,一起学习交流,共同进步!