<分区>
问题是这样的
function demo() {
return new Promise((resolve, reject) => {
...
// The problem here!!
//I just found in some rare case we failed to call resolve or reject
})
}
demo()
.then(res => {
console.log('resolve')
console.log(res)
})
.catch(rej => {
console.log('reject')
console.log(rej)
})
.finally(() => {
console.log('why')
})
当我调用resolve或reject失败时,连finally block 都没有调用!为什么?
我原以为这是一个错误,然后我发现原作者似乎是故意这样做的,如果他没有调用 resolve 或 reject,则不应调用 then/catch/finally,即在那种情况下 不应采取任何后续行动。
但这是否是处理不应采取后续行动的情况的有效方法?会不会有什么问题?
-----更新-----
即使我的问题被标记为重复,我仍然对我得到的答案不满意。本来我认为让 promise 永远停留在 pending 状态是个坏主意。
但是那个 SO 中的答案说“应该没有副作用。”
Does never resolved promise cause memory leak?还说“简而言之 - 至少在现代浏览器中 - 只要你没有对它们的外部引用,你就不必担心 Unresolved promise ”。因此,如果这是目的,让 promise 保持待处理似乎是可以的。