Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
workaround optimization killers
var errorObject = {value: null};
function tryCatch(fn, ctx, args) {
try {
return fn.apply(ctx, args);
}
catch(e) {
errorObject.value = e;
return errorObject;
}
}
var result = tryCatch(mightThrow, void 0, [1,2,3]);
//Unambiguously tells whether the call threw
if(result === errorObject) {
var error = errorObject.value;
}
else {
//result is the returned value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment