Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a jQuery plugin I've made by modifying the actual core code for <code>$.when()</code> to use your semantics. For want of a better name it's called <code>$.myWhen()</code>:</p> <pre><code>(function($) { $.myWhen = function( subordinate /* , ..., subordinateN */ ) { var i = 0, responseValues = Array.prototype.slice.call( arguments ), length = responseValues.length, // the count of uncompleted subordinates remaining = length !== 1 || ( subordinate &amp;&amp; jQuery.isFunction( subordinate.promise ) ) ? length : 0, // the master Deferred. If responseValues consist of only a single Deferred, just use that. deferred = remaining === 1 ? subordinate : jQuery.Deferred(), // Update function for all resolve, reject and progress values updateFunc = function( i, contexts, values ) { return function( value ) { contexts[ i ] = this; values[ i ] = arguments.length &gt; 1 ? Array.prototype.slice.call( arguments ) : value; if( values === progressValues ) { deferred.notifyWith( contexts, values ); } else if ( !( --remaining ) ) { deferred.resolveWith( contexts, values ); } }; }, progressValues, progressContexts, responseContexts; // add listeners to Deferred subordinates; treat others as resolved if ( length &gt; 1 ) { progressValues = new Array( length ); progressContexts = new Array( length ); responseContexts = new Array( length ); for ( ; i &lt; length; i++ ) { if ( responseValues[ i ] &amp;&amp; jQuery.isFunction( responseValues[ i ].promise ) ) { responseValues[ i ].promise() .always( updateFunc( i, responseContexts, responseValues ) ) .progress( updateFunc( i, progressContexts, progressValues ) ); } else { --remaining; } } } // if we're not waiting on anything, resolve the master if ( !remaining ) { deferred.resolveWith( responseContexts, responseValues ); } return deferred.promise(); }; })(jQuery); </code></pre> <p>Just put this code right after where you've loaded jQuery and the <code>$.myWhen()</code> function will be available alongside <code>$.when()</code>. Everything else is 100% exactly the same except for the semantics.</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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