title: Call a function and wait until response is returned before continuing?
body: <p>Attempting to work with functions that return true or false, and also have some ajax included.</p>
  
  <p>I call a function expecting a true or false reply:</p>
  
  <pre><code>if(stepIsValid(stepName) == true) {
  //Do Something
  }
  else doSomethingElse();
  </code></pre>
  
  <p>The function 'stepIsValid()' does a number of checks and then requests additional info via AJAX.  Then returns true or false:    </p>
  
  <pre><code>function stepIsValid(stepName) {
      var isValid = true;
  
      if(condition) {
          isValid = false;
      }
      if(condition) {
          isValid = false;
      }
      if(condition) {
          isValid = false;
      }
      if (pub &amp;&amp; isbn) {
          $.post( "/index.php/mypath", { isbn: isbn, name: pub })
              .done(function( data ) {
                  $("#resultElement").val(data);
                  isValid = true;
                  return isValid;
              })
             .error(function( data ) {
                  isValid = false;
                  return isValid;
             });
      }
      else {
          isValid = false;
          return isValid;
      }
  </code></pre>
  
  <p>My problem is that when stepIsValid is called, jQuery doesn't wait for a response, assumes false and calls doSomethingElse() before true is returned.</p>
  
  <p>What is the best way to overcome this problem?</p>
  
  <p>Code does in fact return true but not quick enough.</p>
  
  <p>Both functions are in separate files.</p>
  
  <p>(assume condition, pub, isbn are all plumbed in and available)</p>