返回新的Promise((resolve,reject)=> {})忘记调用resolve或reject时会发生什么? [英] What will happen when return new Promise((resolve, reject) => {}) forgot to call either resolve or reject?

查看:616
本文介绍了返回新的Promise((resolve,reject)=> {})忘记调用resolve或reject时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是这样的

  function demo(){
return new Promise((resolve,reject) => {
...
//这里的问题!
//我只是在极少数情况下发现我们无法调用解决或拒绝
})
}

demo()
.then(res => {
console.log('resolve')
console.log(res)
})
.catch(rej => {
console.log('reject')
console.log(rej)
})
.finally (()=> {
console.log('why')
})

当我无法调用解决或拒绝时,甚至都不会调用finally块!为什么?



我曾经以为是个错误,所以我发现原始作者似乎是故意这样做的,如果他既不叫解决也不拒绝,那/ catch / finally应该被调用,即在这种情况下不采取后续行动



但这是一种有效的方法处理不采取后续行动的情况?



-----更新-----



问题被标记为重复,我对所获得的答案仍然不满意。最初,我认为让诺言永远保持在待决状态是个坏主意。



但是这样的回答说应该没有副作用。 br>
从未解决的诺言会导致内存泄漏吗?还说:总之,至少在现代浏览器中,只要没有外部引用,您就不必担心未解决的承诺。因此,如果这是目的,那么让承诺保留在待处理状态似乎是可以的。

解决方案

在内部,承诺可以位于以下一种三种状态:



待处理,当最终值尚不可用时。这是唯一可能转换为其他两个状态之一的状态。
已完成,何时以及是否有最终值可用。履行价值与承诺永久相关。这可以是任何值,包括未定义。
已拒绝,如果错误导致无法确定最终值。拒绝原因与承诺永久相关。这可能是任何值,包括未定义的值,尽管它通常是一个Error对象,例如在异常处理中。



https://developer.mozilla.org/zh-CN/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise



在您的情况下,promise处于待处理状态,调用demo函数将始终等待promise状态被实现或拒绝。 p>

The problem is like this

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')
    })

When I failed to call resolve or reject, even the finally block is not called! Why ?

I had thought it was a bug then I found the original author seemed to do that on purpose that if he did not call either resolve or reject, none of then/catch/finally should be called, i.e. in that case no follow-up action should be taken.

But is this a valid way to handle the situation that no follow-up action should be taken ? Will it cause any trouble ?

----- update -----

Even though my question was marked duplicated I am still not satisfied with the answers I got. Originally I had thought it was a bad idea to let promise stay in pending state forever.

But the answer in that SO said "There should be no side effect."
Does never resolved promise cause memory leak? also said "In short - at least in modern browsers - you don't have to worry about unresolved promises as long as you don't have external references to them". So it seems ok to let promise stay in pending if that is the purpose.

解决方案

Internally, a promise can be in one of three states:

Pending, when the final value is not available yet. This is the only state that may transition to one of the other two states. Fulfilled, when and if the final value becomes available. A fulfillment value becomes permanently associated with the promise. This may be any value, including undefined. Rejected, if an error prevented the final value from being determined. A rejection reason becomes permanently associated with the promise. This may be any value, including undefined, though it is generally an Error object, like in exception handling.

https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise

In your case, the promise is in the pending state and calling demo function will always in wait for the promise status to be fulfilled or rejected.

这篇关于返回新的Promise((resolve,reject)=> {})忘记调用resolve或reject时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆