In Javascript, what is better than try / catch to exit the outer scope?

In Javascript, I sometimes want to return a value from a scope that is not the current function. It can be a block of code inside a function, or it can be a closing function, as in the following example, which uses a local function to recursively search for something. Once it finds a solution, the search is done and the external function should just exit. Unfortunately, I cannot come up with a simpler way to do this than to hack try / catch for this purpose:

function solve(searchSpace) {
    var search = function (stuff) {
        var solution = isItSolved(stuff);
        if (solution) {
            throw solution;
        } else {
            search(narrowThisWay(stuff));
            search(narrowThatWay(stuff));
        };
    };
    try {
        return search(searchSpace);
    } catch (solution) {
        return solution;
    };
};

I understand that you can assign a solution to a local variable and then test it before making another recursive call, but my question is about transferring control. Is there a better way than higher? Perhaps using label / break?

: "ew that bad ", . , Javascript. , , , , " ". , . , Javascript .

, - , .

+5
4

, . , , . , goto , . , , try-catch goto. , , if :

function solve(searchSpace) {
    function search = function (stuff) {
        //|| will only return the first value if that value is truthy, subsequent values will be ignored
        return isItSolved(stuff) || (search(narrowThisWay(stuff)) || search(narrowThatWay(stuff)));
    };
    return search(searchSpace);
};

, , . , , , . , , JavaScript ,

+2
function solve(stuff) {
    return isItSolved(stuff) || solve(narrowThisWay(stuff)) ||     solve(narrowThatWay(stuff));
}

... , ( )... , , .

PS: , isItSolved, narrowThisWay narrowThatWay , false, . ?: , ! == undefined.

PS2: , , ...

+2

, . ""?

function solve(searchSpace) {
    var search = function (stuff) {
        var solution = isItSolved(stuff);
        if (solution) {
            return solution;
        } else {
            solution = search(narrowThisWay(stuff));
            if (solution) {
              return solution;
            }
            return search(narrowThatWay(stuff));
        };
    };
    return search(searchSpace);
};

, , , ( ) .

+1

- , JS ( JS- , CPS, ). C setjmp/longjmp. Common Lisp ( ). JS - , .

You can programmatically transform a program into another using CPS.

function solve(searchSpace, isItSolved, isBase, narrowThisWay, narrowThatWay) {
    function search(stuff, k) {
        solution = isItSolved(stuff);
        if (solution) {
            return solution;
        } else if (isBase(stuff)) {
            return k();
        } else {
            return search(narrowThisWay(stuff), function() {
                    return search(narrowThatWay(stuff), k);
                });
        };
    };
    return search(searchSpace, function(val) {return val});
};


var arr=[1, 2,9,72,0,34,5,33,24,62,89,90,30,54,590,23,59,62,73];

solve(arr, function(a) {return (a.length==1 && a[0] == 5) ? a[0] : false;},
      function (a) {return a.length < 2; },
      function (a) {return a.slice(0, a.length / 2);}, 
      function (a) {return a.slice(a.length / 2);}
    );
+1
source

All Articles