javascript - Strange Unhandled promise rejection -


can't understand why behavior different. in version works expected:

const debug = require("debug")("m");  const promise = new promise((resolve, reject) => {     settimeout(() => {         reject("promise rejected");     }, 1000); });  promise.then(     v => {         debug("resolve", v);     },     e => {         debug("reject", e);     }, ); 

put catch handler instead of reject handler:

const debug = require("debug")("m");  const promise = new promise((resolve, reject) => {     settimeout(() => {         reject("promise rejected");     }, 1000); });  promise.then(v => {     debug("resolve", v); });  promise.catch(e => {     debug("catch: ", e); }) 

works same, nodejs warning unhandledpromiserejectionwarning. how understand this?

one of key things then , catch they create new promises. (see the promises/a+ spec* , the javascript spec ["newpromisecapability" spec's way of saying "create new promise"].) promise then handler in second example creating rejected because underlying promise rejected, , rejection never handled.

the usual way catch chain:

promise     .then(v => {         debug("resolve", v);     })     .catch(e => {         debug("catch: ", e);     }); 

that way, there no unhandled rejection (and errors thrown in then callback, if any, propagated rejection catch).


* promises/a+ allows then return same promise provided implementation meets other requirements, javascript's promises don't.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -