Javascript 来自Try内调用的承诺的未处理承诺拒绝
我有一个名为Javascript 来自Try内调用的承诺的未处理承诺拒绝,javascript,node.js,promise,Javascript,Node.js,Promise,我有一个名为findProperty的promise函数,在本例中,该函数拒绝如下内容: reject('ERR: Unknown propertyID or inactive property in call of newBooking'); 这是我的处理程序,它调用findProperty: async function main() { var res = ""; try { findProperty(); res = await fi
findProperty
的promise函数,在本例中,该函数拒绝如下内容:
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');
如果
findProperty
返回承诺,则需要等待它,以便在异步函数的上下文中触发失败;否则,拒绝消失在外层空间
要在不等待的情况下“发射并忘记”,同时用您的try/catch
捕捉失败:
findProperty().catch(e => { throw e; });
所以你不能在不想等待回复的情况下使用正常的承诺?我以为你可以混搭。实际上你是对的@torazaburo-一旦我把res=wait find属性()代码>它工作得很好。那么,我的问题是,我该如何尽全力而忘记承诺?我有很多不想等待结果的地方。@torbenrudgaard是的。但是您需要提供一个catch,比如findProperty().catch(console.log.bind(console));或者干脆不要拒绝them@Jonasw啊。。。Okiee。。我想我明白了。所以我给了他们一个单独的捕捉,这样我就可以在控制台上打印或者其他任何东西。我不知道你能做到,谢谢。什么是console.log.bind
?以前从未见过bind?@torbenrudgaard passing console.log只传递log,如果这样做,它会说error,log不是console实例的一部分,这就是为什么需要将console绑定到它。或者您可以:.catch(e=>console.log(e));这不会传递引用,所以没有问题。。。