title: How can I wait until my AJAX request is complete?
body: <p>So, I have an init function that runs when the DOM is fully loaded:</p>
  
  <pre><code>function init() 
  {
      TerrainObj.load_terrain();
      TerrainObj.generate_display_layer(PlayerObj);
      GameObj.update();
      ScreenObj.draw_screen(TerrainObj);
      ScreenObj.update_screen(TerrainObj);
      mainloop();
  }
  </code></pre>
  
  <p>The very first method, <code>TerrainObj.load_terrain()</code>, makes an AJAX requestL:</p>
  
  <pre><code>load_terrain: function()
  {
      var settings = {
          type: "GET",
          url: "data/map.txt",
          aysnc: false
      };
  
      $.ajax(settings).done(function(result) 
      {
          this.terrain = result.split("\n");
          for (var i = 0; i &lt; this.terrain.length; i++)
          {
              this.terrain[i] = this.terrain[i].split("")
          }
          console.log(this.terrain);
      });
  }
  </code></pre>
  
  <p>The problem is that, in our <code>init()</code> function, most of the methods that follow <code>TerrainObj.load_terrain()</code> require the presence of the <code>TerrainObj.terrain array</code>, but they are called before the AJAX request can complete and that variable is populated. I put a little message out when each function runs to check the order, and I get this:</p>
  
  <pre><code>Ran TerrainObj.load_terrain()
  Ran ScreenObj.draw_screen()
  Uncaught TypeError: Cannot read property '0' of undefined 
  [Array[55], Array[55], ... ]
  </code></pre>
  
  <p>So as you can probably see, <code>ScreenObj.draw_screen</code>, which needs TerrainObj.terrain, has run before the request can complete, and there's a <code>TypeError</code> when it tries to reference our object. It's not ready yet! The very last message is the <code>console.log()</code> from the <code>load_terrain</code> method, indicating that the <code>.done</code> function runs afterwards.</p>
  
  <p>What can I do to wait until the AJAX request is complete before continuing on with my program?</p>