Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The workarounds above (thanks!) don't properly address the problem of getting back the objects provided to the deferred's <code>resolve()</code> method because jQuery calls the <code>done()</code> and <code>fail()</code> callbacks with individual parameters, not an array. That means we have to use the <code>arguments</code> pseudo-array to get all the resolved/rejected objects returned by the array of deferreds, which is ugly:</p> <pre class="lang-js prettyprint-override"><code>$.when.apply($,deferreds).then(function() { var objects=arguments; // The array of resolved objects as a pseudo-array ... }; </code></pre> <p>Since we passed in an array of deferreds, it would be nice to get back an array of results. It would also be nice to get back an actual array instead of a pseudo-array so we can use methods like <code>Array.sort()</code>.</p> <p>Here is a solution inspired by <strong>when.js</strong>'s <code>when.all()</code> method that addresses these problems:</p> <pre class="lang-js prettyprint-override"><code>// Put somewhere in your scripting environment if (typeof jQuery.when.all === 'undefined') { jQuery.when.all = function (deferreds) { return $.Deferred(function (def) { $.when.apply(jQuery, deferreds).then( function () { def.resolveWith(this, [Array.prototype.slice.call(arguments)]); }, function () { def.rejectWith(this, [Array.prototype.slice.call(arguments)]); }); }); } } </code></pre> <p>Now you can simply pass in an array of deferreds/promises and get back an array of resolved/rejected objects in your callback, like so:</p> <pre><code>$.when.all(deferreds).then(function(objects) { console.log("Resolved objects:", objects); }); </code></pre>
 

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