Wait for a function to finish before if()else()
<p>I have this:</p>

<pre><code>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");
    }
}
</code></pre>

<p>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.</p>

<p>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.</p>

<p>And, I am trying to keep the getNames() as generic as possible. So, I am thinking of a way to not disturb the getNames().</p>

<p>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().</p>

<p>Any thoughts guys?</p>

<p>Thanks.</p>
