title: Control flow with XmlHttpRequest?
body: <p>XmlHttpRequest works through callbacks. So how can I return a value? I tried to set a global variable, but that doesn't seem to be working.</p>
  
  <pre><code>var response = null; // contains the most recent XmlHttpRequest response
  
  // loads the info for this username on the page
  function loadUsernameInfo(username) {
      getUserInfo(username);
      var profile = response;
      if (profile) {
          // do stuff
      }
      else {
          indicateInvalidUsername(username);
      }
  }
  </code></pre>
  
  <p><code>getUserInfo()</code> can't return a result, because of the callback:</p>
  
  <pre><code>function getUserInfo(username) {
      var request = createRequest();
      request.onreadystatechange = userObjFromJSON;
      var twitterURL = "http://twitter.com/users/show/" + escape(username) + ".json";
      var url = "url.php?url=" + twitterURL;
      request.open("GET", url, true);
      request.send(null);
  }
  </code></pre>
  
  <p>The callback:</p>
  
  <pre><code>function userObjFromJSON() {
      if (this.readyState == 4) {
          alert(this.responseText);
          response = this.responseText;
      }
  }
  </code></pre>
  
  <p>How can I get the response back to <code>loadUsernameInfo()</code>?</p>