title: How can I return an AJAX-retrieved value to the parent function of the current function in JavaScript?
body: <p>I have the following JavaScript (and jQuery) code:</p>
  
  <pre><code>function checkEmail(email) {
      if (email.length) {
          $.getJSON('ajax/validate', {email: email}, function(data){
              if (data == false) {
                  // stuff
              }
              return data;
          })
      }
  }
  </code></pre>
  
  <p>I want the anonymous function to <code>return data</code> to the parent function, <code>checkEmail()</code>.  I tried doing something like this:</p>
  
  <pre><code>function checkEmail(email) {
      if (email.length) {
          var ret = null;
          $.getJSON('ajax/validate', {email: email}, function(data){
              if (data == false) {
                  // stuff
              }
              ret = data;
          })
          return ret;
      }
  }
  </code></pre>
  
  <p>But of course this won't work because the <code>$.getJSON()</code> call is asynchronous, so it will <code>return ret</code> before the GET request is finished.</p>
  
  <p>Any thoughts here?</p>
  
  <p>Thank you!</p>