Note that there are some explanatory texts on larger screens.

plurals
  1. POQueuing optional AJAX/function calls in jQuery ($.Deferred)?
    text
    copied!<p>I'm looking to queue an arbitrary number of possibly optional function calls in JavaScript/jQuery. For example, I may need to ensure the user is authenticated and a cookie is set before running a second (or third, fourth, etc.) function or AJAX call.</p> <p>I looked into doing this with the recently added jQuery.Deferred, but found that it doesn't matter what order the calls are fired in (true async style). Also, I read that once a Deferred instance has been resolved, it's not possible to un-resolve it.</p> <p>Here's where I'm at with this at the moment. Initially, I was thinking of setting the Deferred instance to resolved, then un-resolving it if an optional function came up in the stack.</p> <pre><code>var d = $.Deferred(), chained = d, d.resolve(), div = extra.find( "div:first" ); if ( extra.attr( "requires-auth" ) != undefined &amp;&amp; !config.user_is_authenticated ) chained = chained.pipe( authenticate ); if ( div.length ) chained = chained.pipe( prepareExtra( div ) ); // When the two optional methods are resolved, show the content chained.done( function() { extra.fadeIn( 500 ) } ); </code></pre> <p>My question is, what is the best way to queue (0 to N) AJAX calls in pure JavaScript/jQuery? (without using plug-ins).</p> <p>Tak!</p> <p><strong>Edit 2: SOLVED!</strong> Here's some working examples of this, one w/o AJAX and one with: <a href="https://gist.github.com/1021429" rel="nofollow">https://gist.github.com/1021429</a> <a href="https://gist.github.com/1021435" rel="nofollow">https://gist.github.com/1021435</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload