节点 8.6 javascript 承诺:UnhandledPromiseRejectionWarning [英] node 8.6 javascript promises: UnhandledPromiseRejectionWarning
问题描述
我有一个错误:(node:6186) UnhandledPromiseRejectionWarning: Unhandled promise Rejection (rejection id: 1): Threep(节点:6186)[DEP0018] 弃用警告:不推荐使用未处理的承诺拒绝.将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程.-------- 那些======== 两个CaughtCathchError 三点(node:6186) PromiseRejectionHandledWarning: Promise 拒绝被异步处理 (rejection id: 1)我正在以嵌套顺序使用我的 3 个承诺函数.p1,p2,p3- 是我的承诺函数,如下所示.我也尝试在所有 p1、p2、p3 函数中添加承诺拒绝,但它仍然相同
I am having an error: (node:6186) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): threep (node:6186) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. -------- ones ========= two CaughtCathchError threep (node:6186) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1) I am consuming my 3 promise functions in nested order. p1,p2,p3- are my promise functions as shown below. i tried adding promise reject in all p1,p2,p3 functions as well but its still the same
enter code here
var p1 = new Promise(function (resolve, reject) {
setTimeout(function () {
// resolve('ones')
resolve('ones')
}, 9000)
})
var p2 = new Promise(function (resolve, reject) {
setTimeout(function () {
// throw new Error('eeeee');
//reject('two')
resolve('two')
}, 1000)
})
var p3 = new Promise(function (resolve, reject) {
setTimeout(function () {
reject('three')
}, 4000)
})
p1.then(function(result){
console.log("--------", result)
// return p2.then(function(res){console.log(res)}).catch(function(err){console.log(err)})
return p2
}).then(function(p2result){
console.log("=========", p2result)
return p3;
}).then(function(p3result){
console.log('*********', p3result)
}).catch(function(err){
console.log("CaughtCathchError", err)
})
推荐答案
p3
是一个 独立的 Promise,没有 .catch
.所以,当 p3
得到 reject
ed 时,你会得到一个 UnhandledPromiseRejectionWarning
.即使 p3
在具有正确 catch
的 Promise
链中稍后被消耗,p3
本身没有 catch
.
p3
is a standalone Promise with no .catch
. So, when p3
gets reject
ed, you get a UnhandledPromiseRejectionWarning
. Even if p3
is consumed later in a Promise
chain that has a proper catch
, p3
itself does not have a catch
.
代替 p3
,您可以使用返回 Promise
的 函数,并确保捕获对该函数的所有调用:
Instead of p3
, you might use a function that returns a Promise
, and ensure that all calls to that function are caught:
var p1 = new Promise(function (resolve, reject) {
setTimeout(function () {
// resolve('ones')
resolve('ones')
}, 1000)
})
var p2 = new Promise(function (resolve, reject) {
setTimeout(function () {
// throw new Error('eeeee');
//reject('two')
resolve('two')
}, 1000)
})
var getp3 = () => new Promise(function (resolve, reject) {
setTimeout(function () {
reject('three')
}, 1000)
})
p1.then(function(result){
console.log("--------", result)
// return p2.then(function(res){console.log(res)}).catch(function(err){console.log(err)})
return p2
}).then(function(p2result){
console.log("=========", p2result)
return getp3();
}).then(function(p3result){
console.log('*********', p3result)
}).catch(function(err){
console.log("CaughtCathchError", err)
})
如果你需要立即初始化p3
,那么在p3
本身之后放一个catch
.
If you need to initialize p3
immediately, then put a catch
after the p3
itself.
这篇关于节点 8.6 javascript 承诺:UnhandledPromiseRejectionWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!