javascript - In express.js, when I use promises, do I need to include reject() in the executor? -
in situation not deal reject, must include reject in promise executor?
for example:
new promise ((res)=>{ res(a); })
if know promise never rejected, adding rejection handler not necessary. e.g. it's obvious never rejected , adding rejection handler plain silly:
promise.resolve("abc") .then(result => console.log(result));
in other cases, not providing rejection handler pretty same not wrapping error-throwing piece of code in try
-catch
. might intentional, of course, bear in mind node.js treat unhandled promise rejections if uncaught errors: terminating node.js process. that's says on console when unhandled promise rejection occurs:
(node:7741) deprecationwarning: unhandled promise rejections deprecated. in future, promise rejections not handled terminate node.js process non-zero exit code.
so, summing up:
- skip rejection handlers when know fact rejection impossible,
- skip rejection handlers when require application terminated upon rejection (in future versions of node.js),
- write rejection handlers in other cases.
edit (the question became clearer after edits made, hence update answer):
as including reject()
call in promise executor, no, not need add it. again though, have make sure internal logic guarantees resolve()
called.
Comments
Post a Comment