我有一个名为findProperty
的承诺函数,在这种情况下拒绝这样:
reject('ERR: Unknown propertyID or inactive property in call of newBooking');
这是我的调用findProperty
的处理程序:
async function main() {
var res = "";
try {
findProperty();
res = await findUser(userEmail);
res = await findUser(agentEmail);
res = await getUsers();
}
catch(err) {
console.error(err);
console.log(" newBooking: " + err);
callback( { error:true, err } );
}
}
main();
这会导致以下错误:
(node:10276) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ERR: Unknown propertyID or inactive property in call of newBooking
(node:10276) [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.
我没有得到这个,我得到了我的catch(err)
应该不够吗?
试过一些东西,这很好用:
resolve('ERR: Unknown propertyID or inactive property in call of newBooking');
但这会产生错误:
reject('ERR: Unknown propertyID or inactive property in call of newBooking');
答案 0 :(得分:2)
如果findProperty
返回一个promise,则需要await
才能在异步函数的上下文中触发失败;否则拒绝消失在外太空。
在没有等待的情况下“发射并忘记”,但却用try/catch
findProperty().catch(e => { throw e; });