Promise reject error - Unhandled rejection Error: closed
Promise reject error - Unhandled rejection Error: closed
我使用以下代码检查某些应用程序端口当前是否正常工作,但我在第三个 else 语句中出错
Unhandled rejection Error: Port is not open
我该如何处理?
我用蓝鸟
checkPortStatus: function(port, host){
return new Promise((resolve, reject) => {
portscanner.checkPortStatus(port, host, function(error, status) {
if(error)
reject(error);
else if(status === 'open')
resolve(status);
else
reject(new Error('Port is not open'));
});
});
},
最终,您需要处理被拒绝的承诺,例如使用 .catch()
:
obj.checkPortStatus(port, host).then((status) => {
...
}).catch((err) => {
// handle the error here...
});
调用 checkPortStatus 的代码的属性导致未处理的异常。
该代码可能看起来像
somePromiseFunction()
.then(checkPortStatus(port, host))
.then(someOtherPromiseFunction())
如果它看起来更像
,它会(至少)"handle"例外
somePromiseFunction()
.then(checkPortStatus(port, host))
.catch(function(error) {
console.log(error.message);
})
.then(someOtherPromiseFunction()
你的代码在这方面有问题:使用resolve
和reject
时,还需要使用return
。所以不要使用 resolve()
,而是使用 return resolve()
;与 reject
.
相同
附注,以防有帮助:添加我提到的 return
后,代码中的每个 else
语句前面都会紧跟着一个 return
。您可以删除 else
语句。
祝你好运!
我使用以下代码检查某些应用程序端口当前是否正常工作,但我在第三个 else 语句中出错
Unhandled rejection Error: Port is not open
我该如何处理? 我用蓝鸟
checkPortStatus: function(port, host){
return new Promise((resolve, reject) => {
portscanner.checkPortStatus(port, host, function(error, status) {
if(error)
reject(error);
else if(status === 'open')
resolve(status);
else
reject(new Error('Port is not open'));
});
});
},
最终,您需要处理被拒绝的承诺,例如使用 .catch()
:
obj.checkPortStatus(port, host).then((status) => {
...
}).catch((err) => {
// handle the error here...
});
调用 checkPortStatus 的代码的属性导致未处理的异常。
该代码可能看起来像
somePromiseFunction()
.then(checkPortStatus(port, host))
.then(someOtherPromiseFunction())
如果它看起来更像
,它会(至少)"handle"例外somePromiseFunction()
.then(checkPortStatus(port, host))
.catch(function(error) {
console.log(error.message);
})
.then(someOtherPromiseFunction()
你的代码在这方面有问题:使用resolve
和reject
时,还需要使用return
。所以不要使用 resolve()
,而是使用 return resolve()
;与 reject
.
附注,以防有帮助:添加我提到的 return
后,代码中的每个 else
语句前面都会紧跟着一个 return
。您可以删除 else
语句。
祝你好运!