2

I wrote these simple code,

function a()
{
    function b()
    {
        function d()
        {
            if (/*required condition goes here and should stop execution of
              function a()*/)
            {
                return
            }
        }

        d()
    }
    function c()
    {
        alert ('c')
    };
    b();
    c();
}

What I expected is the execution aborts when it meet the required condition on function d() and cancelling the execution of function c(). Otherwise, it runs the remaining function after executing function b().
How can I do that?

Edit: What I actually want to know wheter if the 'return' key colud terminate immediately for the outer function like 'Exit Sub' on VB.Net

1
  • function d could return a value which you match later to then return if it matches an expected value. Commented Feb 18, 2017 at 12:31

2 Answers 2

1

You'd pass the information that processing should stop out of d and out of b and use it to determine whether to call c:

function a() {
    function b() {
        function d() {
            if (/*some condition*/) {
                return false;       // ***
            }
            return true;            // ***
        }
        return d();                 // ***
    }

    function c() {
        alert('c')
    };

    if (b()) {                      // ***
        c();                        // ***
    }                               // ***
}

Alternately, you could have d throw:

function a() {
    function b() {
        function d() {
            if (/*some condition*/) {
                throw new Error();     // ***
            }
            return;
        }
        d();
    }

    function c() {
        alert('c')
    };

    b();
    c();
}

...but then a would throw rather than completing normally (though you could add a try/catch). Also, throwing is relatively expensive and not for normal branching purposes.

Sign up to request clarification or add additional context in comments.

4 Comments

So, it mean the 'return' keyword doesn't stop the execution immediately for the outer function, like 'Exit Sub' on VB.Net?
@BayuSadewa: return only exits the function you use it in, not its containing function. I'm 99.99% sure it's the same for Exit Sub in VB.Net when you use a lambda within another function (it's certainly the same for return in C#.Net if you use a lambda within another function).
OK now I see. I would use the value from inner function to determine whether to call remaining function on the container/outer function, am I right ? @T.J. Crowder
Thanks for your answer, nice explanation. That was the most closer to what I need to know. Anyway, how can i mark this question as solved? I'm newbie here.
0

You could use the return value for the inner function and use it as flag.

function a() {
    function b() {
        function d() {
            console.log('in d');
            if (true) {
                return;
            }
            return true;
        }
        console.log('in b');
        return d();
    }

    function c() {
        console.log('in c');
        alert('c');
    }

    console.log('in a');
    console.log(b() && c());
}

a();

7 Comments

What does exactly b() && c() do?
it is a logical AND && and checks the first expression and if it has a truthy value, the check the second expression. in this case, if undefined or false is returned, the evaluation stops.
According to snippet code above, what we got is b() return true from its inner function d(), and c() goes undefined, becausec() never determined what it returned. So, the flag of b() && c() return false. Am I right? @ninascholz
b returns d and d returns undefined. b() && c() returns undefined from b and returns this value.
Whoa, i missed the d() Hahah. So, what about the alert('c'). It never been executed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.