const PROMISEPENDIONG = "PROMISEPENDIONG";
const PROMISEFULFILLED = "PROMISEFULFILLED";
const PROMISEREJECTED = "PROMISEREJECTED";
class MyPromise {
constructor(exetuator){
exetuator(this.resolve, this.reject);
}
status = PROMISEPENDIONG;
sucessValue = undefined;
failReason = undefined;
resolve = value => {
if(this.status == PROMISEPENDIONG) {
this.status = PROMISEFULFILLED;
this.sucessValue = value;
}
}
reject = reason => {
if(this.status == PROMISEPENDIONG) {
this.status = PROMISEREJECTED;
this.failReason = reason;
}
}
then(sucessCallBack, failCallBack) {
if(this.status == PROMISEFULFILLED) {
sucessCallBack(this.sucessValue);
} else if(this.status == PROMISEREJECTED){
failCallBack(this.failReason);
}
}
}
module.exports = MyPromise;
const PROMISEPENDIONG = "PROMISEPENDIONG";
const PROMISEFULFILLED = "PROMISEFULFILLED";
const PROMISEREJECTED = "PROMISEREJECTED";
class MyPromise {
constructor(exetuator){
exetuator(this.resolve, this.reject);
}
// 状态值
status = PROMISEPENDIONG;
// 成功结果
sucessValue = undefined;
// 失败原因
failReason = undefined;
// 成功回调
sucessCallBack = undefined;
// 失败回调
failCallBack = undefined;
resolve = value => {
if(this.status == PROMISEPENDIONG) {
this.status = PROMISEFULFILLED;
this.sucessValue = value;
// 若有缓存的成功回调则回调
this.sucessCallBack && this.sucessCallBack(this.sucessValue);
this.sucessCallBack = undefined;
}
}
reject = reason => {
if(this.status == PROMISEPENDIONG) {
this.status = PROMISEREJECTED;
this.failReason = reason;
// 若有缓存的失败回调则回调
this.failCallBack && this.failCallBack(this.sucessValue);
this.failCallBack = undefined;
}
}
then(sucessCallBack, failCallBack) {
if(this.status == PROMISEFULFILLED) {
sucessCallBack(this.sucessValue);
} else if(this.status == PROMISEREJECTED){
failCallBack(this.failReason);
} else {
this.sucessCallBack = sucessCallBack;
this.failCallBack = failCallBack;
}
}
}
module.exports = MyPromise;
const PROMISEPENDIONG = "PROMISEPENDIONG";
const PROMISEFULFILLED = "PROMISEFULFILLED";
const PROMISEREJECTED = "PROMISEREJECTED";
class MyPromise {
constructor(exetuator){
exetuator(this.resolve, this.reject);
}
// 状态值
status = PROMISEPENDIONG;
// 成功结果
sucessValue = undefined;
// 失败原因
failReason = undefined;
// 成功回调
sucessCallBack = [];
// 失败回调
failCallBack = [];
resolve = value => {
if(this.status == PROMISEPENDIONG) {
this.status = PROMISEFULFILLED;
this.sucessValue = value;
// 若有缓存的成功回调则回调
while(this.sucessCallBack.length) this.sucessCallBack.shift()(this.sucessValue);
}
}
reject = reason => {
if(this.status == PROMISEPENDIONG) {
this.status = PROMISEREJECTED;
this.failReason = reason;
// 若有缓存的失败回调则回调
while(this.failCallBack.length) this.failCallBack.shift()(this.reason);
}
}
then(sucessCallBack, failCallBack) {
if(this.status ==