title: AJAX: Waiting for response?
body: <p>I'm using global variables to pass around a response from a AJAX call:</p>
  
  <pre><code>window.response = null; // most recent response from XMLHttpRequest
  
  // the callback function for XMLHttpRequest
  function userObjFromJSON() {
      if (this.readyState == 4) {
          var json = eval('(' + this.responseText + ')');
          window.response = json;
      }
      else {
          indicateLoading();
      }
  }
  
  // loads the info for this username on the page
  function loadUsernameInfo(username) {
      clearPage();
      getUserInfo(username);
      var profile = window.response; // most recent json response (what if it hasn't come in yet?)
      window.response = null;
      if (profile) {
          indicateLoaded(username);
          fillInProfileInfo(profile);
  
          getTweets(username);
          var tweets = window.response; // most recent json response (what if it hasn't come in yet?)
          if (tweets) {
              fillInTweets(tweets, MAX_TWEETS);
              var mentions = mentionedUsers(tweets, MAX_TWEETS);
              fillInMentioned(mentions);
          }
          else {
              indicateUnavailableTweets();
          }
      }
      else {
          indicateInvalidUsername(username);
      }
  }
  </code></pre>
  
  <p>The problem is that, by the time the controller function wants to start filling in information, the AJAX call hasn't always returned yet. (If I step through it slowly in a debugger, it works fine.) What can I do to get around this?</p>
  
  <p>I tried something like this:</p>
  
  <pre><code>getUserInfo(username);
  while (window.response == null); // infinite loop here
  var profile = window.response; // most recent json response
  </code></pre>
  
  <p>But that just makes my browser unresponsive.</p>
  
  <p>I am hesitant to call the needed function from the callback, because I'm trying to implement model-view-controller. Calling a controller/view function from the model feels like it would break the pattern.</p>