I have this:
var myError; function getNames(user,location){ myError=null; //Make an Ajax Call (this takes some time) //On Success do this: if(//Some Condition){ myError=false; }else{ myError=true } } function first(){ getNames(user, location); if(myError==false){ //MYCONDITION //do some custom stuff just for first(); }else{ alert("Failed"); } } function second(){ getNames(user, location); if(myError==false){ //MYCONDITION //do some custom stuff just for second(); }else{ alert("Failed"); } } Now, when I call first() or second() it always gives me alert message "FAILED". The problem is that //MYCONDITION is being executed before the getNames() function finishes. I want to wait for getNames() to execute before I can check //MYCONDITION.
I cannot set a delay since I don't know how much time getNames() is going to take. I was thinking about using some jQquery function but I cannot seem to find one.
And, I am trying to keep the getNames() as generic as possible. So, I am thinking of a way to not disturb the getNames().
The last option I see is adding callback() to getNames(). But, this function is being called by other functions too that won't need callback().
Any thoughts guys?
Thanks.
getNames()function did finish (return). It's just that Ajax requests are asynchronous.