// 为了简化拆解,这里只实现then
class myPromise {
constructor(fn) {
this.cbs = []
const res = (data) => {
this.cbs.forEach(n => {
this.data = data
n(data)
})
}
fn(res)
}
then(onRes) {
return new myPromise((resolve) => {
this.cbs.push(() => {
const res = onRes(this.data)
if (res instanceof myPromise) {
res.then(resolve)
}
else {
resolve(res)
}
})
})
}
}