/**
* 请始终使用catch
* 请始终使用catch
* 请始终使用catch
*/
class PromiseDone {
promise = null
value = undefined
nextId = 0
nextMap = {}
constructor(promise) {
this.promise = promise
}
done(cb) {
this.done = cb
}
then(resolve, reject) {
return this.promise.then(value => {
if (!resolve) return
const returnVal = resolve(value)
if (returnVal) {
this.value = returnVal
this.nextMap[this.nextId++] = returnVal
this.exec()
}
console.log(this.nextMap)
return this.then
}, err => {
if (!reject) return
const returnVal = resolve(value)
if (returnVal) {
this.value = returnVal
this.nextMap[this.nextId++] = returnVal
this.exec()
}
console.log(this.nextMap)
return this.then
})
}
exec() {
}
}
{
const p = new PromiseDone(new Promise(r => r(1)))
p.then(v => {
console.log('v1', v)
return 2
}).then(v => {
console.log('v2', v)
})
p.then(v => {
console.log('v3', v)
}).then(v => {
return new Promise(r => {
setTimeout(r, 1000, 100)
})
}).then(v => {
console.log('v33', v)
})
p.done(() => {
console.log(1)
})
}
{
const p = new Promise(r => r(1))
p.then(v => {
console.log(v)
return 2
}).then(v => {
console.log(v)
})
p.then(v => {
console.log(v)
return v
}).then(v => {
console.log(v)
})
}
console