最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to properly throw an error if promise is rejected? (UnhandledPromiseRejectionWarning) - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 2

You 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.') });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论