MyEarlyExit
Throws error and exits early
by Shawn Stark
HTML
<button id="safetyrun1">Run Safe Function</button>
JavaScript
var funCaller = (function(msg){
console.log(msg);
var curExecution;
var exceptionHandlers = [];
exceptionHandlers.cleanup = function(){alert("set straglers to {}");return;};
return function(fun){
let exNames = [];
var deref = function(){fun={};}
var exitErr = function(errcode){
if(1===1){
deref();
exNames.push('cleanup');
throw new Error("EarlyExit");
}
}
fun = fun(exitErr);
fun = {};
for (var x in exNames){exceptionHandlers[exNames[x]]();}
fun = {};
return;
};
})('selfinvokingfunction')
var exitThisScript = function(exit){
//write some modular code
alert("before exit");
exit();
alert("this works elsewhere and i keep having to change fiddle to keep it working");
return;
}
document.getElementById('safetyrun1').onclick = function() { funCaller(exitThisScript); }