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 exitErr = function(errcode){
							try{
              	if(1===1){throw new Error("EarlyExit");}
							}catch(err){//queue tasks here
               	exNames.push('cleanup');
              }
    				}
            fun(exitErr)
            for (var x in exNames){exceptionHandlers[exNames[x]]();}
            return;
          };            
        })('selfinvokingfunction')

        var exitThisScript = function(exit){
          //write some modular code
          alert("before exit");
          exit();
          alert("after exit");
          return;
        }    
document.getElementById('safetyrun1').onclick = function() { funCaller(exitThisScript); }