class CustomPromise {
state = "PENDING";
value = undefined;
thenCallbacks = [];
errorCallbacks = [];
constructor(action) {
action(this.resolver, this.reject);
}
static resolve = (value) => {
return new Promise((res) => res(val));
}
resolver = (value) => {
this.state = "RESOLVED";
this.value = value;
this.thenCallbacks.forEach((callback) => {
callback(this.value);
});
}
reject = (value) => {
this.state = "REJECTED";
this.value = value;
this.errorCallbacks.forEach((callback) => {
callback(this.value);
});
}
then(callback) {
this.thenCallbacks.push(callback);
return this;
}
catch (callback) {
this.errorCallbacks.push(callback);
return this;
}
}
let promise = new CustomPromise((resolver, reject) => {
setTimeout(() => {
const rand = Math.ceil(Math.random(1 * 1 + 6) * 6);
if (rand > 2) {
resolver("Success");
} else {
reject("Error");
}
}, 1000);
});
promise.then((res) => console.log(res)).catch(err => console.log(err));
console.log("Hello World!");