public
Last active

jQuery's "when" and "then" all rolled up together.

  • Download Gist
jquery.ba-whenthen.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*!
* jQuery whenthen - v0.1 - 3/12/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
 
(function($){
 
/*
$.whenthen( deferred, [ done_callback, [ fail_callback ] ] )
Pass a Deferred, `done` callback, and `fail` callback all at once. Multiple
items of any arg can be specified by passing an array for that arg.
Returns a promise.
*/
$.whenthen = function( whens, dones, fails ) {
var promise,
methods = [$.when, 'done', 'fail'];
 
$.each(arguments, function( i, arg ) {
var method = i ? promise[methods[i]] : methods[i];
promise = method[$.type(arg) === 'array' ? 'apply' : 'call'](promise, arg);
});
 
return promise;
};
 
})(jQuery);
usage.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
Sample Usage:
 
$.whenthen( deferred, function( a ) {
// `a` are the value(s) the deferred was resolved with.
})
 
$.whenthen( array_of_two_deferreds, function( a, b ) {
// `a` and `b` are the value(s) the deferreds were resolved with.
})
 
$.whenthen( deferred, function( a ) {
// `a` are the value(s) the deferred was resolved with.
}, function( a ) {
// `a` are the value(s) the deferred was rejected with.
})
 
... etc ...
 
*/
 
var dfds = [];
$.each("a b c d".split(" "),function( i, v ) {
var dfd = $.Deferred();
setTimeout(function(){
if ( i === 3 ) {
dfd.reject( "rejected " + i );
} else {
dfd.resolve( "resolved " + i );
}
}, 100);
dfds.push(dfd);
});
var dfdYay = dfds[0],
dfdYays = dfds.slice(0, -1),
dfdNay = dfds[3],
dfdNays = dfds.slice(1);
 
function logger( msg ) {
return function() {
console.log(msg, Array.prototype.slice.call(arguments));
}
}
 
// The "jQuery" way.
$.when(dfds[0], dfds[1], dfds[2]).then(logger('jq done 1'), logger('jq fail (n/a)'));
$.when(dfds[0], dfds[1], dfds[2]).done(logger('jq done 2')).fail(logger('jq fail (n/a)'));
$.when(dfds[1], dfds[2], dfds[3]).then(logger('jq done (n/a)'), logger('jq fail 1'));
$.when(dfds[1], dfds[2], dfds[3]).done(logger('jq done (n/a)')).fail(logger('jq fail 2'));
 
// Options!
$.whenthen(dfdYay, logger('wt done 1'));
$.whenthen(dfdYay, [logger('wt done 2'), logger('wt done 3')]);
$.whenthen(dfdYays, [logger('wt done 4'), logger('wt done 5')]);
$.whenthen(dfdYay, logger('wt done 6'), logger('wt fail (n/a)'));
$.whenthen(dfdYays, logger('wt done 7'), [logger('wt fail (n/a)'), logger('wt fail (n/a)')]);
$.whenthen(dfdYay, [logger('wt done 8'), logger('wt done 9')], logger('wt fail (n/a)'));
$.whenthen(dfdYays, [logger('wt done 10'), logger('wt done 11')], [logger('wt fail (n/a)'), logger('wt fail (n/a)')]);
 
$.whenthen(dfdNay, logger('wt done (n/a)'));
$.whenthen(dfdNay, [logger('wt done (n/a)'), logger('wt done (n/a)')]);
$.whenthen(dfdNays, [logger('wt done (n/a)'), logger('wt done (n/a)')]);
$.whenthen(dfdNay, logger('wt done (n/a)'), logger('wt fail 1'));
$.whenthen(dfdNays, logger('wt done (n/a)'), [logger('wt fail 2'), logger('wt fail 3')]);
$.whenthen(dfdNay, [logger('wt done (n/a)'), logger('wt done (n/a)')], logger('wt fail 4'));
$.whenthen(dfdNays, [logger('wt done (n/a)'), logger('wt done (n/a)')], [logger('wt fail 5'), logger('wt fail 6')]);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.