Last active
December 18, 2015 11:09
-
-
Save Kambfhase/5773165 to your computer and use it in GitHub Desktop.
This is a simple way to detect critical erros in synchronous code. Not sure if genuis or stupid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function detect( fn, err){ | |
var flag = true; | |
setTimeout(function(){ | |
if(flag) err(); | |
},0); | |
fn(); | |
flag = false; | |
} | |
// example with error | |
detect( function(){ | |
({}).foo(); | |
}, function(){ | |
console.log("ZOMG, an error occured!"); | |
}); | |
// example without error | |
detect( function(){ | |
return 1+1; | |
}, function(){ | |
// wont be executed | |
console.log("ZOMG, an error occured!"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've tested out the performance of this approach using jsperf.com. I determined two things...
http://jsperf.com/try-catch-vs-settimeout-error-detection