Wednesday, 15 May 2013

javascript - Debugging exceptions in DOM Promises -



javascript - Debugging exceptions in DOM Promises -

not long ago chrome devtools started supporting async stack traces (http://www.html5rocks.com/en/tutorials/developertools/async-call-stack/) can avoid pain of debugging asynchronous code.

but dom promises (http://www.html5rocks.com/en/tutorials/es6/promises/) released, bringing pain right back.

if exception thrown somewhere within promised code swallowed promises scheme , not allow debugger stop if "pause on exceptions" on.

alright, can turn on "pause on caught exceptions" lead pause on every promise rejection redundant. want grab kinds of real javascript or libraries errors showing code written incorrectly. promises may rejected without logic error though:

function showlargeimage (user) { homecoming promise(function (resolve, reject) { if (!user.image.large) { // expected behavior. no exception pause needed. reject('no larger image.'); } else { // if element doesn't exist? want grab exception here. $('#user-' + user.id + '-large-image')[0].style.display = 'block'; resolve(); } }); }

has faced same issue? how debug code?

update: code illustration wrong. exceptions swallowed in "then" handler (not in promise body). should like:

function showlargeimage (user) { homecoming loadlargeimage(user).then(function (largeimage) { if (!largeimage) { // expected behavior. no exception pause needed. homecoming promise.reject('no larger image.'); } else { // if element doesn't exist? want grab dom exception here. $('#user-' + user.id + '-large-image')[0].src = largeimage; homecoming true; } }); }

simple workaround - avoid dom promises. not production ready yet.

this known issue. dom promises @ moment rather experimental:

they much slower fast promise implementations bluebird. they provide no .done method or unhandled rejection detection in chrome. exceptions swallowed silently. they provide limited subset of functionality.

if have utilize dom promises, utilize firefox, version 27+ features unhandled rejection detection based on gc. stack traces still lot worse bluebird's @ to the lowest degree it'll not swallow exceptions silently.

the upside there plans build improve unhandled rejection detection chrome dom promises.

you can utilize bluebird , swap out native promises in production (although outperforms them , really want debuggability in production).

but do current code?

most promise libraries, bluebird can deed drop-in replacement.

javascript asynchronous exception-handling error-handling promise

No comments:

Post a Comment