Skip to content

Instantly share code, notes, and snippets.

@linus-amg
Created August 28, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linus-amg/e3201b0dd7e8a78cbabd to your computer and use it in GitHub Desktop.
Save linus-amg/e3201b0dd7e8a78cbabd to your computer and use it in GitHub Desktop.
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