Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// from the brilliant mind of sb
var _catch = Promise.prototype.catch;
Promise.prototype.catch = function () {
return _catch.call(this, function (err) { setTimeout(function () { throw(err); }, 0); });
}
@RangerMauve

This comment has been minimized.

Copy link

@RangerMauve RangerMauve commented Feb 12, 2016

But... it doesn't actually invoke the callback for .catch....

@darobin

This comment has been minimized.

Copy link
Owner Author

@darobin darobin commented Feb 12, 2016

Isn't that the point? :)

@greim

This comment has been minimized.

Copy link

@greim greim commented Feb 12, 2016

This is like fixing your TV by throwing out the third story window.

@darobin

This comment has been minimized.

Copy link
Owner Author

@darobin darobin commented Feb 12, 2016

@greim Funny you would say that, that is exactly how I fixed my TV!

@geirman

This comment has been minimized.

Copy link

@geirman geirman commented Feb 13, 2016

I'm missing the point, can someone give the 4th grader's explanation of what problem this solves and how?

@moimikey

This comment has been minimized.

Copy link

@moimikey moimikey commented Feb 13, 2016

seems straight forward to me? monkey patching catch to throw caught errors asynchronously to be queued and looped back onto the call stack vs being potentially swallowed?

@moimikey

This comment has been minimized.

Copy link

@moimikey moimikey commented Feb 13, 2016

oh hm. throwing inside catch continues the chain with a rejected promise -> unterminated chain -> swallows errors.

@moimikey

This comment has been minimized.

Copy link

@moimikey moimikey commented Feb 13, 2016

okay. i get it then. so the setTimeout helps to unswallow subsequent errors in the chain, because when you setTimeout, it's async, so it gets popped back onto the call stack again after it gets queued, going through the event loop, and back onto the call stack with an intact stack trace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment