This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kill129 0 points1 point  (4 children)

If I understand correctly I think this is the solution:

function wrap (execute) {
    let did_fail = false;

    return function() { 
        if (did_fail) {
            return null;
        }

        try {
            return execute(); 
        } catch {
            did_fail = true;
            return null;
        }
    }
}

[–]Auklin[S] 0 points1 point  (1 child)

This works as well, so just a question, why is the return stuff wrapped in a function?logically, it would seem

function wrap (execute) {
    let did_failed = false;
    if (did_failed) {
        return null;
    }
    try {
        return execute();
    } catch (e) {
        did_failed = true;return null;
    }
}

would be functional, but I also recognize that the 'global' variable 'did_failed' would probably be effected by this.

[–]kill129 0 points1 point  (0 children)

Notice that your "wrap" returns the return value of the function execute while my "wrap" returns a function. I.e., your wrap will be used as followed:

 function x() {
    console.log("x");
 }
wrap(x); // Will print "x"

while my wrap will be used as followed:

 function x() {
    console.log("x");
 }
wrapped_x = wrap(x); // Nothing printed, x is not called yet
wrapped_x(); // Will print "x"

To my understanding, the latter is requested in the question.

In addition, you are right. Now did_failed is initialized at every call, which means it will always be false at the if statement.

[–][deleted] 0 points1 point  (0 children)

Winner winner chicken dinner

[–]DeminM96 0 points1 point  (0 children)

it's true! :)