Implementing $.when with $.ajax POST <p>I have an AJAX POST function. I want to obtain a successful callback then execute a function. I chose to achieve this with <code>$.when</code> as <a href="http://jsfiddle.net/smacky311/wYGj6/1/" rel="nofollow">follows</a></p> <pre><code>var url = '/echo/html/'; var json_text = ' '; var FireOrderCounter = 0; $.when( $.ajax({ type: 'POST', url: url, data: json_text, success: function () { FireOrderCounter++; alert('successfully completed Action ' + FireOrderCounter); var millisecondsToWait = 5000; setTimeout(function() { FireOrderCounter++; alert('Done Spinnin ' + FireOrderCounter); }, millisecondsToWait); }, dataType: 'html' }) ).then(openWindow()); function openWindow() { FireOrderCounter++; alert('opened window' + FireOrderCounter ); } </code></pre> <p><code>success</code> callback fires after <code>openWindow()</code>. Does this mean that <code>$.ajax</code> is somehow not deferred and <code>$.when</code> is simply <a href="http://api.jquery.com/jQuery.when/" rel="nofollow">assuming success</a> as described in the API?</p> <blockquote> <p>If a single argument is passed to jQuery.when and it is not a Deferred, it will be treated as a resolved</p> </blockquote> <p>This is a simplified test case. The production code fails similarly. I have to fire this event twice to obtain all the data. There is a race condition.</p> <p>I can insert a breakpoint to make the processing stop. The Function fires while I am still holding onto the debugger. So, its not waiting for a success callback. A short timeout (occurs after ~10 seconds)? How can I fix this?</p>