getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
179.25k stars 33.48k forks source link

"async & performance": Document unhandled rejection hooks #348

Open benjamingr opened 9 years ago

benjamingr commented 9 years ago

In async & performance you mention promise rejections and debuggability with error handling. A patch has landed in io.js in 1.4 which is released tomorrow with native promises that lets you do something similar to your example:

Your example syntax:

var p = Promise.reject( "Oops" ).defer();
var p2 = Promise.reject( "Oops" ); // will be reported as an unhandled rejection to the console

Can now be coded with

var p = Promise.reject( "Oops" ).catch(function(){});
var p2 = Promise.reject( "Oops" ); // will be reported as an unhandled rejection to the console

When doing:

process.on("unhandledRejection", function(promise, reason) {
    console.log("Unhandled Error", reason);
});

This will achieve semantics very close to your .defer semantics using native ES6 promises in io.js, I think it's worth mentioning :) How do you feel about this?

getify commented 9 years ago

Unfortunately, the book is already off to press now, so it's too late to sneak in anything for first edition. But it's worth keeping this around to see how it shakes out. AFAIK, this unhandledRejection thing is not ES6 spec related, but a hosting-environment extension. I'd want to document it if it fully takes off as the de facto standard of how promise rejections are handled across browsers and ssjs's.

benjamingr commented 9 years ago

Yeah, it's not ES6 spec related - it's implemented on the serverside and there is a similar proposal for window.onerror

benjamingr commented 8 years ago

Just updating that it's also in the DOM now as window.addEventListener("unhandledrejection", ...

getify commented 8 years ago

thanks!