admin管理员组文章数量:1406943
I have a promise and I would like an exception to be thrown if the promise is rejected. I tried this:
var p = new Promise( (resolve, reject) => {
reject ("Error!");
} );
p.then(value => {console.log(value);});
but I get a DeprecationWarning:
(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.
What is the correct way to throw an error (so that the program is terminated with a stack trace) if the promise is rejected?
I already tried to insert a throw statement in a catch clause, but this again produces a DeprecationWarning as before. In fact (after some reading) I understand that a throw in a catch produce another call to the reject callback.
I have a promise and I would like an exception to be thrown if the promise is rejected. I tried this:
var p = new Promise( (resolve, reject) => {
reject ("Error!");
} );
p.then(value => {console.log(value);});
but I get a DeprecationWarning:
(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.
What is the correct way to throw an error (so that the program is terminated with a stack trace) if the promise is rejected?
I already tried to insert a throw statement in a catch clause, but this again produces a DeprecationWarning as before. In fact (after some reading) I understand that a throw in a catch produce another call to the reject callback.
Share Improve this question edited Aug 19, 2017 at 12:03 Emanuele Paolini asked Aug 19, 2017 at 11:31 Emanuele PaoliniEmanuele Paolini 10.2k5 gold badges45 silver badges69 bronze badges 3- 1 "I would like an exception to be thrown" - What? Why? To where? Where would you want to catch it? – Bergi Commented Aug 19, 2017 at 12:17
- I would like to terminate the program and present a stack trace to the user... as the future behaviour. – Emanuele Paolini Commented Aug 19, 2017 at 12:55
- See also stackoverflow./a/30741722/918910. – jib Commented Aug 19, 2017 at 21:13
3 Answers
Reset to default 2You can catch unhandledRejection
events to log an stack trace, provided that you reject using a proper Error
:
var p = new Promise( (resolve, reject) => {
reject( Error("Error!") );
} );
p.then(value => {console.log(value);});
process.on('unhandledRejection', e => {
console.error(e);
});
…so that the program is terminated with a stack trace if the promise is rejected?
That's exactly what unhandled promise rejections will do in the future, as the "deprecation" warning is telling you. See these pull requests for what they plan to do, as well as the general discussion.
For now, you can listen to unhandledRejection
events to do this:
process.on('unhandledRejection', err => {
console.error(err); // or err.stack and err.message or whatever you want
process.exit(1);
});
You are getting DeprecationWarning because adding catch block
while resolving promise is going to be mandatory.
You can throw the error from inside catch block, this way your program will be terminated with the error's stack trace, like:
p.then( value => console.log(value) ).catch( e => { throw e });
Else you can catch the error and do some stuff while not terminating the process, like:
p.then( value => console.log(value) ).catch( e => { console.log('got an error, but the process is not terminated.') });
本文标签:
版权声明:本文标题:javascript - how to properly throw an error if promise is rejected? (UnhandledPromiseRejectionWarning) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744938101a2633311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论