const PENDING = 'PENDING';
const FULFILLED = 'FULFILLED';
const REJECTED = 'REJECTED';
const resolvePromise = (promise2, x, resolve, reject) => {
// 自己等待自己完成是错误的实现,用一个类型错误,结束掉 promise Promise/A+ 2.3.1
if (promise2 === x) {
return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
}
// Promise/A+ 2.3.3.3.3 只能调用一次
let called;
// 后续的条件要严格判断 保证代码能和别的库一起使用
if ((typeof x === 'object' && x != null) || typeof x === 'function') {
try {
// 为了判断 resolve 过的就不用再 reject 了(比如 reject 和 resolve 同时调用的时候) Promise/A+ 2.3.3.1
let then = x.then;
if (typeof then === 'function') {
// 不要写成 x.then,直接 then.call 就可以了 因为 x.then 会再次取值,Object.defineProperty Promise/A+ 2.3.3.3
then.call(x, y => { // 根据 promise 的状态决定是成功还是失败
if (called) return;
called = true;
// 递归解析的过程(因为可能 promise 中还有 promise) Promise/A+ 2.3.3.3.1
resolvePromise(promise2, y, resolve, reject);
}, r => {
// 只要失败就失败 Promise/A+ 2.3.3.3.2
if (called) return;
called = true;
reject(r);
});
} else {
// 如果 x.then 是个普通值就直接返回 resolve 作为结果 Promise/A+ 2.3.3.4
resolve(x);
}
} catch (e) {
// Promise/A+ 2.3.3.2
if (called) return;
called = true;
reject(e)
}
} else {
// 如果 x 是个普通值就直接返回 resolve 作为结果 Promise/A+ 2.3.4
resolve(x)
}
}
class Promise {
constructor(executor) {
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
this.onFulfilledCallbacks = [];
this.onRejectCallbacks = [];
let resolve = (value) => {
if (this.status == PENDING) {
this.status = FULFILLED;
this.value = value;
this.onFulfilledCallbacks.forEach(fn => fn())
}
};
let reject = (reason) => {
if (this.status == PENDING) {
this.status = REJECTED;
this.reason = reason;
this.onRejectCallbacks.forEach(fn => fn())
}
}
try {
executor(resolve, reject);
} catch(error) {
reject(error);
}
}
static resolve(data) {
return new Promise((resolve, reject) => {
resolve(data);
});
}
static reject(reason) {
return new Promise((resolve, reject) => {
reject(reason);
});
}
then(onFulfilled, onReject) {
onFulfilled = typeof onFulfilled == 'Function' ? onFulfilled : v => v;
onReject = typeof onReject == 'Function' ? onReject : error => { throw error};
let promise2 = new Promise((resolve, reject) => {
if (this.status == FULFILLED) {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
} catch(error) {
reject(error);
}
}, 0)
}
if (this.status == REJECTED) {
setTimeout(() => {
try {
let x = onReject(this.reason);
} catch(error) {
reject(error);
}
}, 0)
}
if (this.status == PENDING) {
this.onFulfilledCallbacks.push(() => {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
} catch(error) {
reject(error);
}
}, 0)
});
this.onRejectCallbacks.push(() => {
setTimeout(() => {
try {
let x = onReject(this.reason);
} catch(error) {
reject(error);
}
}, 0)
});
}
});
return promise2;
}
}
Promise.all = function(values) {
if (!Array.isArray(values)) {
const type = typeof values;
return new TypeError(`TypeError: ${type} ${values} is not iterable`)
}
return new Promise((resolve, reject) => {
let resultArr = [];
let orderIndex = 0;
const processResultByKey = (value, index) => {
resultArr[index] = value;
if (++orderIndex === values.length) {
resolve(resultArr)
}
}
for (let i = 0; i < values.length; i++) {
let value = values[i];
if (value && typeof value.then === 'function') {
value.then((value) => {
processResultByKey(value, i);
}, reject);
} else {
processResultByKey(value, i);
}
}
});
}
Promise.race = function(promises) {
return new Promise((resolve, reject) => {
// 一起执行就是for循环
for (let i = 0; i < promises.length; i++) {
let val = promises[i];
if (val && typeof val.then === 'function') {
val.then(resolve, reject);
} else { // 普通值
resolve(val)
}
}
});
}
console