【发布时间】:2018-01-27 23:43:50
【问题描述】:
我有一个承诺,如果承诺被拒绝,我希望抛出一个异常。我试过这个:
var p = new Promise( (resolve, reject) => {
reject ("Error!");
} );
p.then(value => {console.log(value);});
但我收到了弃用警告:
(node:44056) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error!
(node:44056) 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.
如果 promise 被拒绝,抛出错误的正确方法是什么(以便程序以堆栈跟踪终止)?
我已经尝试在 catch 子句中插入一个 throw 语句,但这又像以前一样产生了 DeprecationWarning。事实上(经过一些阅读)我知道抛出一个 catch 会产生另一个对拒绝回调的调用。
【问题讨论】:
-
"我想抛出一个异常" - 什么?为什么?去哪里?你想在哪里抓到它?
-
我想终止程序并向用户显示堆栈跟踪...作为未来的行为。