我正在处理承诺以及如何设置它们,但是我不明白为什么节点认为我的承诺在错误方面未得到处理...有人可以解释一下吗?
我的简单代码
// Setting up the Promise
function add(x, y) {
return new Promise((response, reject) => {
// Simple conditional (not perfect, but it just proves a point)
if(x !== null && y !== null) {
// I know I could have done 'response(x + y)', but I wanted
// to console.log the result also
var calc = x + y
response(calc)
console.log('Calculation: ' + x + ' + ' + y + ' = ' + calc)
} else {
// My console does not throw this error?
reject('One of the inputs was null')
}
})
}
// Function using the Promise
function calc() {
add(1, 3)
.then(res => add(res, 3))
.then(res => add(res, null))
.then(res => console.log('Final result: '+res))
.catch(err => {
// This error is thrown in console
throw new Error('Something went horribly wrong')
})
}
// Run the code
calc();
更新
我最初发布的'拒绝'有一个抛出的错误,我明白需要抓住它。
我还想了解为什么在我的控制台中看不到“拒绝”中的字符串?
控制台输出:
Calculation: 1 + 3 = 4
Calculation: 4 + 3 = 7
(node:61950) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Something went horribly wrong
(node:61950) [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.
答案 0 :(得分:1)
在使用Promise的函数中:
// Function using the Promise
function calc() {
add(1, 3)
.then(res => add(res, 3))
.then(res => add(res, null))
.then(res => console.log('Final result: '+res))
.catch(err => {
// This error is thrown in console
---> throw new Error('Something went horribly wrong')
})
}
与--->
合作的行会抛出错误。那个错误没有被发现。通常当你发现错误时,你想用它做一些事情。如果你把它扔回去或抛出另一个错误,那么应该抓住那个投掷。
我对此的处理如下:
// Function using the Promise
function calc() {
return add(1, 3)
.then(res => add(res, 3))
.then(res => add(res, null))
.then(res => console.log('Final result: '+res))
.catch(err => {
// This error is thrown in console
throw new Error('Something went horribly wrong')
})
}
calc().catch(err => {
console.log(error.message); // For example
});
答案 1 :(得分:1)
您正在调用reject
和投掷错误。你只需要做其中一个。
您没有显示第一条错误消息。
当您致电.catch
时,使用add
来解决任何抛出错误也很重要。
当你这样做时:
throw new Error('Something went horribly wrong')
你仍然在promise
,如果你没有抓到它,你就会造成问题。在Node.js的未来版本中,这将导致您的应用程序退出并显示错误代码。因此,您需要确保始终在承诺中捕获抛出的错误。
使用拒绝
// Setting up the Promise
function add(x, y) {
return new Promise((response, reject) => {
// Simple conditional (not perfect, but it just proves a point)
if(x !== null && y !== null) {
// I know I could have done 'response(x + y)', but I wanted
// to console.log the result also
var calc = x + y
response(calc)
console.log('Calculation: ' + x + ' + ' + y + ' = ' + calc)
} else {
// My console does not throw this error?
reject(new Error('One of the inputs was null'))
}
})
}
// Function using the Promise
function calc() {
return add(1, 3)
.then(res => add(res, 3))
.then(res => add(res, null))
.then(res => console.log('Final result: '+res))
.catch(err => {
console.error("Internal Error:", err.message);
// This error is thrown in console
throw new Error('Something went horribly wrong')
})
}
// Run the code
calc().catch((err) => {
console.error("Outer error:", err.message);
});
使用投掷:
// Setting up the Promise
function add(x, y) {
return new Promise((response, reject) => {
// Simple conditional (not perfect, but it just proves a point)
if(x !== null && y !== null) {
// I know I could have done 'response(x + y)', but I wanted
// to console.log the result also
var calc = x + y
response(calc)
console.log('Calculation: ' + x + ' + ' + y + ' = ' + calc)
} else {
// My console does not throw this error?
throw new Error('One of the inputs was null')
}
})
}
// Function using the Promise
function calc() {
return add(1, 3)
.then(res => add(res, 3))
.then(res => add(res, null))
.then(res => console.log('Final result: '+res))
.catch(err => {
console.error("Internal Error:", err.message);
// This error is thrown in console
throw new Error('Something went horribly wrong')
})
}
// Run the code
calc().catch((err) => {
console.error("Outer error:", err.message);
});
我添加了return
add
返回的承诺catch
,因此您可以try-catch
最终错误。
另一种方法是不抛出最终错误并以不同方式处理它。