2

Let's say I have function below

var isallow = false; function checkToAllow(){ if(isallow == false) // stop execution of calling method( parent method) } function showMEVal(){ checkToAllow(); console.log("I am here because I am allowed"); } 

so basicaly, showMEVal() method will first check that execution further is allowed or not, if it is allowed it will continue further, else it will exit.

This is to be common functionality to a number a function where it needs to be first checked whether it should be continue or not.

I dont want to explicitly work with returned values from checkToAllow() method, like

 function showMEVal(){ if(checkToAllow() == true) console.log("I am here because I am allowed"); } 

I was thinking to use event.stopImmediatePropagation(); but it does not seems it will work.

Please advise

Thanks

3
  • Yes i saw that post, please have a look on marked as answer post Commented Dec 26, 2014 at 11:02
  • function showMEVal() { if (!checkToAllow()) return; console.log("I am here because I am allowed"); } Commented Dec 26, 2014 at 11:02
  • It seems no one interested in reading the questions fully, I am getting answer what i marked in question that does not suits my scenario Commented Dec 26, 2014 at 11:04

1 Answer 1

2

The normal way to handle this is to throw an exception in the child function. You can read a brief description of exception handling in JavaScript at w3schools, here: http://www.w3schools.com/js/js_errors.asp.

So for your example, you'd have:

var isallow = false; function checkToAllow() { if(isallow == false) throw CustomAbort(); // Can be anything: object or basic data type } function showMEVal(){ try{ checkToAllow(); } catch (err) { return; } console.log("I am here because I am allowed"); } 

What makes this useful is that you only have to have the try... catch... construct at the very top level of your JavaScript, and only once. Then, deep down in your processing you can simply have:

function deepFunction() { checkToAllow(); console.log("I am here because I am allowed."); console.log("I'm not sure what happens if I'm not allowed, because it's taken care of by checkToAllow(), and by the top level error handling!") } 

You could probably also just throw anything from checkToAllow and provided nothing else is catching that error, it would propagate all the way up as a JavaScript error and stop the rest of your processing... but that's obviously not best practice, because no one wants JavaScript errors in their page!

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

6 Comments

I started with a half-assed answer and then elaborated.
Not exactly, but could be best here
It required to use checkToAllow(); in try catch in every method i use
Well I saw when i use checkToAllow(); only without try catch it simply throws error in console, but seems solve the problem, isn't it?
Use checkToAllow(); wherever you want to check if something is allowed. If you want to check in particular low-level function, put it there. If you only want to check once, put it somewhere in your top-level function.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.