简体   繁体   中英

How to trigger an early exit from outside function?

I'm trying to create a function that's responsible for checking a boolean and exiting early with a warning, if true.

Here's example of what i'm trying to achieve:

function warnAndDie(shouldDie) {
    if(shouldDie) {
        console.log("go away, i'm dying!");
        // TODO: some code to cause the calling function to exit early
    }
}

function triggerTheWarnAndDie() {
    shouldWarnAndDie(true);
    console.log("I should never run!");
}

function dontTriggerTheWarnAndDie() {
    shouldWarnAndDie(false);
    console.log("I should run!");
}

What can i do so that warnAndDie is able to cause the calling functions to terminate early?

thank you

You have several options. I'll list the two very basic ones for you:

  1. Return a value (probably boolean) and return early from you caller depending on the initial return value

     function shouldWarnAndDie(shouldDie) { if(shouldDie) { console.log("go away, i'm dying!"); return true; } return false; } function triggerTheWarnAndDie() { var hasDied = shouldWarnAndDie(true); if (hasDied) return; console.log("I should never run!"); } 
  2. Throw an exception

     function shouldWarnAndDie(shouldDie) { if(shouldDie) { throw "Go away, i'm dying!"; // or cleaner: // throw new dyingException("Go away, i'm dying!"); } } function triggerTheWarnAndDie() { try { shouldWarnAndDie(true); console.log("I should never run!"); } catch(err) { console.log(err+" He's dead Jim!"); } } 

There are more advance mechanics which are probably out of scope for you right now, but LINQ's nice answer about callbacks and promises is definitely worth a look.

This can be achieved with basic exception handling. Here I have created a custom exception which can be caught using a try catch statement.

function shouldWarnAndDie(shouldDie) {
    if(shouldDie) {
        throw new DyingException();
    }
}

function triggerTheWarnAndDie() {
    try {
        shouldWarnAndDie(true);
    } catch(err) {
        console.log(err.message);
        return;
    }
    console.log("I should never run!");
}

function dontTriggerTheWarnAndDie() {
    try {
        shouldWarnAndDie(false);
    } catch(err) {
        console.log(err.message);
        return;
    }
    console.log("I should run!");
}

// custom Exception handler
function DyingException() {
    // do some custom exception handling here
    return new Error("Go away, I am dying!");
}

triggerTheWarnAndDie();     // Go away, I am dying!
dontTriggerTheWarnAndDie(); // I should run!

Here is a JsFiddle Example

use the

return;

keyword to exit

*This is just a quick workaround. You may want to look into error checking and exceptions...

I'd suggest using promises but its not supported natively in all browsers. We can use callbacks where the code inside the callback only gets executed when warnAndDie allows it to execute.

function warnAndDie(shouldDie, fn) {
    if(shouldDie) {
        console.log("go away, i'm dying!");
        // TODO: some code to cause the calling function to exit early
        return;
    }
    fn();
}

function triggerTheWarnAndDie() {
    shouldWarnAndDie(true, function () {
        console.log("I should never run!");
    } );
}

function dontTriggerTheWarnAndDie() {
    shouldWarnAndDie(false, function () {
       console.log("I should run!");
    } );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM