手写Promise,实现基本功能
const PENDING = 'PENDING';
const FULFILLED = 'FULFILLED';
const REJECTED = 'REJECTED';
class MyPromise {
constructor(executor) {
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
this.onResolvedCallbacks = [];
this.onRejectedCallbacks = [];
const resolve = (value) => {
if (this.status === PENDING) {
this.status = FULFILLED;
this.value = value;
console.log(this.onResolvedCallbacks)
this.onResolvedCallbacks.forEach(fn => fn());
}
}
const reject = (reason) => {
if (this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
console.log(this.onRejectedCallbacks)
this.onRejectedCallbacks.forEach(fn => fn());
}
}
try {
executor(resolve, reject);
} catch(err) {
reject(err)
}
}
then(onFulFilled, onRejected) {
if (this.status === FULFILLED) {
onFulFilled(this.value);
}
if (this.status === REJECTED) {
onRejected(this.reason);
}
if (this.status === PENDING) {
this.onResolvedCallbacks.push(() => {
onFulFilled(this.value);
});
this.onRejectedCallbacks.push(() => {
onRejected(this.reason);
})
}
}
}
运行测试
const p1 = new MyPromise(function(resolve, reject) {
resolve(1234)
})
p1.then((value) => {
console.log('success:', value)
}, (err) => {
console.log('error:', err)
})
console.log(p1)

const p2 = new MyPromise(function(resolve, reject) {
reject(new Error('错误'))
})
p2.then((value) => {
console.log('success:', value)
}, (err) => {
console.log('error:', err)
})
console.log(p2)

const p3 = new MyPromise(function(resolve, reject) {
setTimeout(() => {
resolve('setTimeout success')
}, 1000)
})
p3.then((value) => {
console.log('success:', value)
}, (err) => {
console.log('error:', err)
})
console.log(p3)
