Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery: what happens when you pass a deferred object into "then"?
    primarykey
    data
    text
    <p>I have been researching jquery deferred objects for a while now and I'm stumped on one thing.</p> <p>The "done," "always," "then," etc methods take, as their arguments, functions which should be called when the deferred object is resolved.</p> <p>However I tried chaining requests by passing a deferred object into the "always" method, and that <em>seems</em> to work too:</p> <pre><code>// A is an array of data var req = ajax(url + "?d=" + A[0]).done(processResults).fail(reportFailure); for (var i = 1 ; i &lt; A.length ; i++) { var tmp = ajax(url + "?d=" + A[i]).done(processResults).fail(reportFailure); req.always(tmp); req = tmp; } </code></pre> <p>However, it doesn't really work. Because when I follow the above code with this:</p> <pre><code>req.always(foobar); </code></pre> <p>foobar seems to get called at some random time, not necessarily after the last element in the array is processed.</p> <p>Would it be better to use when?</p> <pre><code>// A is an array of data var req = $.when(ajax(url + "?d=" + A[0]).done(processResults).fail(reportFailure)); for (var i = 1 ; i &lt; A.length ; i++) { req = $.when(req, ajax(url + "?d=" + A[i]).done(processResults).fail(reportFailure)); } req.always(foobar); </code></pre> <p>Would the above code (using "when") result in the ajax requests happening one after the other, or would they occur simultaneously?</p> <p>I looked into chaining with "pipe" by the way, but because of scoping issues I think it would be harder to use "pipe" with a for loop as above.</p> <p>Also, why does the original code "almost work"? What is happening there? Is it executing the deferred object as if it were a function, and what happens when it does that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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