Domanda

It is possible to return from/exit the caller function i JavaScript?

We are adding testing methods to our framework and we want to exit if we have an error on an Assertion.

Given the following simple example code i would like to exchange EXIT_TEST_THAT_CALLED_ME for something that will exit ( equivalent to doing return in the test ) the method Test123.

Assert = function ( expectedTrue, comment ) {
    if( ! expectedTrue ) {
      Log.Error( comment );
      EXIT_TEST_THAT_CALLED_ME;    
    }
}

Test123 ( ... ) {

  var inputField = getInputField(".input");
  Assert( (inputField !== null ), "Checking that we found an input field");

  inputField.doSomething();
  // This row will cause run time error if inputField is null  

}

What I have tried
return - Of course not works since it only returns from the Assert Method
arguments.callee.caller.return - Does nothing
arguments.callee.caller.return() - Has no method 'return'

Note
Changing to a Test Framework which already has this and where everything is nice is not an option. :(

È stato utile?

Soluzione

One option would be to use try\catch\throw

Assert = function ( expectedTrue, comment ) {
    if( ! expectedTrue ) {
      Log.Error( comment );
      throw "OOPS";
    }
}

try{
  // function that users Assert
  Test123 ( ... )
}

catch(err){       
    alert(err);
    if (err=="OOPS"){
    //special handling for a certain exception
    }
}

Using this flow, the moment the exception is thrown, it will propagate upwards until it reaches the catch block for you to handle the exception.

Also, the exception isn't limited to be a string. It can be an integer, Boolean or an object.

Altri suggerimenti

It is not possible to change execution flow of your caller, and that is by design.

What you can do is:

  • throw an exception. It will bubble until it is catched somewhere, quickly exiting callers.
  • change your test function to make a conditional return, something like if (!assert(…)) return;.

I do not think it is possible to exit caller from a function. I'd suggest some workarounds:

1.Return a value with your Assert and then do return in caller:

Assert = function ( expectedTrue, comment ) {
            if( ! expectedTrue ) {
              Log.Error( comment );
              return false;    
            }
            return true;
        }    
Test123 ( ... ) {
        var assertResult = Assert( (inputField !== null ), "Checking that we found an input field");
        if (!assertResult)
            return;
        inputField.doSomething();
    }

2.Send a callback function into your Assert:

Assert = function ( expectedTrue, comment, success) {
            if( ! expectedTrue ) {
              Log.Error( comment );
              return;    
            }
            success();
         } 
assertSuccess = function () {
   inputField.doSomething();
}
Test123 ( ... ) {
    Assert( (inputField !== null ), "Checking that we found an input field", assertSuccess);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top