title: Is there a way to ensure that the first AJAX call completes before the subsequent call executes?
body: <p>I am attempting to make consecuitve async ajax calls in order to paint user's schedules into a HTML table via jQuery.
  Each response returns a JSON serialized DataSet containing two tables: one of scheduled events and the other contains user info.  </p>
  
  <p>The problem I am having is that the user info seems to get mixed up with user events. That is, sometimes the user onfo doesn't 
  change for different responses so the scheduled events are tied to an incorrect user. If I set the AJAX async property to false, all is well.</p>
  
  <p>The whole point is to display the schedules one by one as the data is returned instead of making the page freeze until all data is returned.</p>
  
  <p>Is there a way to ensure that the first JAX call completes before the subsequent call executes?</p>
  
  <p>(Perhaps my understanding of setting async to false is incorrrect. Doesn't it mean that all data is harvested before code execution continues?)</p>
  
  <p>Here is my current approach:</p>
  
  <pre><code>        //  When page loads
      $(document).ready(function () {
          // Get date range            
          debugger;
      //GetStartDate(), GetEndDate() populates date range
      //PopulateParams() does that for remaining parameters
          $.when(GetStartDate(), GetEndDate())
          .then(function () {
              PopulateParams();
              GetUserSchedule();
          })
          .fail(function () {
              failureAlertMsg();
  
          })
      });
  
      // Returns schedule for each person listed in between selected start and end dates
      function GetUserSchedule() {
           for (var i = 0; i &lt; arrRequests.length; i++) {
              $.when(
              // Ajax call to web method, passing in string
              $.ajax({
                  type: "POST",
                  url: URL/default.aspx/GetSchedules",
                  data: arrRequests[i],   // example data: {"UserId":"6115","startDate":"\"7/1/2011\"","endDate":"\"7/31/2011\MoreVals: Vals}                    contentType: "application/json",
                  dataType: "json",
                  success: SuccessFunction,
                  error: function (d) { alert('Failed' + d.responseText + '\nPlease refresh page to try again or contact administrator'); }
              })
              )
              .then(function () {
  
              }
              );
          }
      }
  
      // On successful completion of call to web method, paint schedules into HTML table for each user
      function SuccessFunction(data) {            
          if (data != null &amp;&amp; data.d != null &amp;&amp; data.d.Temp != null) {
  
          // Calls a bunch of functions to paint schedule onto HTML table
          // Data contains two tables: one contains user info and the other contains rows of info for each event for user
          // at times, the user info is not the correct user or the events are not correct for user
      }
  </code></pre>