const PENDING = 'PENDING';
const FULFILLED = 'FULFILLED';
const REJECTED = 'REJECTED';
const resolvePromise = (promise2, x, resolve, reject) => {
if (promise2 === x) {
return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
}
let called;
if ((typeof x === 'object' && x != null) || typeof x === 'function') {
try {
let then = x.then;
if (typeof then === 'function') {
then.call(x, y => {
if (called) return;
called = true;
resolvePromise(promise2, y, resolve, reject);
}, r => {
if (called) return;
called = true;
reject(r);
});
} else {
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e)
}
} else {
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 (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