6

I need to terminate the javascript from a function so that it doesn't execute the next block of codes and other functions called by the script, which follows the current function.

I found this existing question in SO : How to terminate the script in Javascript , where the solution is given as to trigger an error / exception which will terminate the script immediately.

But my case is different because, My purpose of terminating the script is to prevent an error which will come from the next functions, if the current function doesn't stop execution of the script. So, triggering another error to prevent an error is no way solving my purpose!

So, is there any other way of terminating the javascript instead of throwing an error / exception?

Please read my question properly before marking as duplicate, I have already explained why it doesn't solve my problem with the question I referred!.

Community
  • 1
  • 1
  • Possible duplicate of [How to terminate the script in Javascript](http://stackoverflow.com/questions/550574/how-to-terminate-the-script-in-javascript) – David Hoelzer Jan 12 '16 at 04:17
  • 2
    Your requirement is unreasonable in the browser environment. You'll need to rearrange your logic so that you can just not call the functions that'll error out. Or set a flag that they check and return early if it's set or something. – Chris Tavares Jan 12 '16 at 04:21
  • I read it completely, Tareq. Didn't you read the suggestions here and all of the answers there? Create a control structure in a `main` function and return to it from your function. You then have the power to decide if you will continue operations or gracefully allow the script to terminate. – David Hoelzer Jan 12 '16 at 04:28
  • Is using jQuery an option ? – guest271314 Jan 12 '16 at 04:28
  • @guest271314, Sure, if possible without error. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 12 '16 at 04:28
  • @DavidHoelzer, I already replied you there, "Terminate Javascript without error / exception", my title clearly explains what I want, which is no way duplicate of the question mentioned and the control structure solution is only acceptable if there is no other simple solution available. I just need to be confirmed on that. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 12 '16 at 04:33
  • Then you didn't read all of the answers. One of them is precisely what Ic answered. – David Hoelzer Jan 12 '16 at 04:52
  • @DavidHoelzer I don't know which one you are trying to mention about, Can you just share the link of the answer here? – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 12 '16 at 05:00
  • And you are trying to say, we shouldn't look for better solution, if anything is achieved by any type of way. For example, we shouldn't look for a function like `Array.map()` if the same thing can be achieved by doing a trick with `Array.forEach()`? – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 12 '16 at 05:32

2 Answers2

5

Since you're inside a function, just return.

Reading into your question a little more that it doesn't execute "other functions called by the script", you should return a boolean. For example, return false if you don't want the rest called, then wrap the remaining statements in an if:

function Foo() {
    if (weAreInLotsOfTrouble) {
        return false;
    }

    return true;
}

DoSomething();
DoSomethingElse();

if (Foo()) {
    DoSomethingDangerous();
}
lc.
  • 113,939
  • 20
  • 158
  • 187
  • 3
    That will only terminate the function, not the script, I need to stop the functions following the current function to stop executing! – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 12 '16 at 04:16
  • So return a true/false from the script and have your main function decide whether or not to proceed based on the return value. – David Hoelzer Jan 12 '16 at 04:16
  • 1
    @DavidHoelzer, Yeah, that's a possible solution. We can do that way in PHP too, but still we have a function called `exit()`, which reduces our task. I just wanted to know, if there is any way to kill the script without error in Javascript, if not, we can find other solutions anyway. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 12 '16 at 04:23
0

Try using jQuery.Callbacks() with parameter "stopOnFalse" ; jQuery.when() , jQuery .then()

var callbacks = $.Callbacks("stopOnFalse");

function a(msg) {
  var msg = msg || "a";
  console.log(msg);
  return msg
}
// stop callbacks here
function b() {
  var msg = false;
  console.log(msg)
  return msg
}
// `c` should not be called
function c() {
  var msg = new Error("def");
  console.log(msg)
  return msg
}

callbacks.add(a,b,c);

$.when(callbacks.fire())
.then(function(cb) {
  // `c` : `Error`
  if (cb.has(c)) {
    console.log("callbacks has `c`"
                , cb.has(c));
    // remove `c`
    cb.remove(c);
    console.log(cb.has(c));
    // empty `callbacks`
    cb.empty();
    // do other stuff
    cb.add(a);
    cb.fire("complete")
    
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
guest271314
  • 1
  • 15
  • 104
  • 177