代码拉取完成,页面将自动刷新
const RESOLVE = 'resolved'
const REJECT = 'rejected'
const PENDING = 'pending'
const handlePromise = (result, newPromise, resolve, reject) => {
if (result === newPromise) {
throw new Error('can not return oneself')
}
if ((typeof result === 'object ' && typeof result !== null) || typeof result === 'function')) {
const then = result.then
if (typeof then === 'function') {
}
} else {
resolve(result)
}
}
class JJPromise {
status = PENDING
result = undefined
reason = undefined
onResolvedArr = []
onRejectedArr = []
constructor(execution) {
const resolve = (result) => {
if (this.status === PENDING) {
this.result = result
this.status = RESOLVE
this.onResolvedArr.map((fn) => fn())
}
}
const reject = (reason) => {
if (this.status === PENDING) {
this.reason = reason
this.status = REJECT
this.onResolvedArr.map((fn) => fn())
}
}
execution(resolve, reject)
}
then(onResolved, onRejected) {
const newPromise = new JJPromise((resolved, reject) => {
if (this.status === RESOLVE) {
setTimeout(() => {
const result = onResolved(this.result)
handlePromise(result, newPromise, resolve, reject)
}, 0);
}
if (this.status === REJECT) {
setTimeout(() => {
const result = onRejected(this.reason)
handlePromise(result, newPromise, resolve, reject)
}, 0);
}
if (this.status === PENDING) {
this.onResolvedArr.push(() => {
onResolved(this.result)
handlePromise(result, newPromise, resolve, reject)
})
this.onRejectedArr.push(() => {
onRejected(this.result)
handlePromise(result, newPromise, resolve, reject)
})
}
})
return newPromise
}
}
module.exports = JJPromise
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。