title: Waiting on multiple asynchronous calls to complete before continuing
body: <p>So, I have a page that loads and through jquery.get makes several requests to populate drop downs with their values.</p>
  
  <pre><code>$(function() {
      LoadCategories($('#Category'));
      LoadPositions($('#Position'));
      LoadDepartments($('#Department'));
  
      LoadContact();
  };
  </code></pre>
  
  <p>It then calls LoadContact(); Which does another call, and when it returns it populates all the fields on the form.  The problem is that often, the dropdowns aren't all populated, and thus, it can't set them to the correct value.</p>
  
  <p>What I need to be able to do, is somehow have LoadContact only execute once the other methods are complete and callbacks done executing.</p>
  
  <p>But, I don't want to have to put a bunch of flags in the end of the drop down population callbacks, that I then check, and have to have a recursive setTimeout call checking, prior to calling LoadContact();</p>
  
  <p>Is there something in jQuery that allows me to say, "Execute this, when all of these are done."?</p>
  
  <p><strong>More Info</strong>
  I am thinking something along these lines</p>
  
  <pre><code>$().executeAfter(
      function () {   // When these are done
          LoadCategories($('#Category'));
          LoadPositions($('#Position'));
          LoadDepartments($('#Department'));
      },
      LoadContact // Do this
  );
  </code></pre>
  
  <p>...it would need to keep track of the ajax calls that happen during the execution of the methods, and when they are all complete, call LoadContact;</p>
  
  <p>If I knew how to intercept ajax that are being made in that function, I could probably write a jQuery extension to do this. </p>
  
  <p><strong>My Solution</strong></p>
  
  <pre><code>;(function($) {
      $.fn.executeAfter = function(methods, callback) {
  
          var stack = [];
  
          var trackAjaxSend = function(event, XMLHttpRequest, ajaxOptions) {
              var url = ajaxOptions.url;
  
              stack.push(url);
          }
  
          var trackAjaxComplete = function(event, XMLHttpRequest, ajaxOptions) {
              var url = ajaxOptions.url;
  
              var index = jQuery.inArray(url, stack);
  
              if (index &gt;= 0) {
                  stack.splice(index, 1);
              }
  
              if (stack.length == 0) {
                  callback();
                  $this.unbind("ajaxComplete");
              }
          }
  
          var $this = $(this);
  
          $this.ajaxSend(trackAjaxSend)
          $this.ajaxComplete(trackAjaxComplete)
  
          methods();
          $this.unbind("ajaxSend");
      };
  })(jQuery);
  </code></pre>
  
  <p>This binds to the ajaxSend event while the methods are being called and keeps a list of urls (<strong>need a better unique id though</strong>) that are called.  It then unbinds from ajaxSend so only the requests we care about are tracked.  It also binds to ajaxComplete and removes items from the stack as they return.  When the stack reaches zero, it executes our callback, and unbinds the ajaxComplete event.</p>