title: Any better way than setTimeout to wait for asnyc callbacks when testing JavaScript?
body: <p>Given this code:</p>
  
  <pre><code>showForm = function (url) {
          return $.get(url, function (html) {
               $('body').append();              
          });
   };
  </code></pre>
  
  <p>When using <code>sinon.js</code>, <code>jQuery.mockjax.js</code> and <code>expect.js</code>, I have the following passing test:</p>
  
  <pre><code>it("showForm calls jQuery.append", function () {
  
      $.mockjax({
            url: '/fake'                    
      });
  
      var spy = sinon.spy($.fn, "append");             
      presenter.showForm('/fake');
  
      setTimeout(function () { 
          expect(spy.called).to.be.equal(true);
      }, 1000);
  });
  </code></pre>
  
  <p>Using the <code>setTimeout</code> function to wait on the callback from the asynchronous <code>$.get</code> smells bad and it will slow down my test suite if I do too much of it.</p>
  
  <p>However, I feel that the intent is reasonably clear and it does seem to test exactly what I want.</p>
  
  <p>Is there a better way and can you please explain what's going on in your answer?</p>