Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 12, 2011 13:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cowboy/867252 to your computer and use it in GitHub Desktop.
Save cowboy/867252 to your computer and use it in GitHub Desktop.
jQuery's "when" and "then" all rolled up together.
/*!
* jQuery whenthen - v0.2 - 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.
Note: when passing a single deferred, if you are unsure whether it is a
promise or a single value that might be an array, you must disambiguate it
by surrounding it with [] (thus explicitly passing it as an array).
*/
$.whenthen = function( whens, dones, fails ) {
var promise;
$.each(arguments, function( i, arg ) {
var method = i ? promise[i === 1 ? 'done' : 'fail'] : $.when;
promise = method[$.type(arg) === 'array' ? 'apply' : 'call'](promise, arg);
});
return promise;
};
})(jQuery);
// Unit tests
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),
testArr = [],
testSuccess = 0;
function test( val ) {
if ( val ) {
testSuccess++;
}
return function() {
testArr.push(val);
}
}
setTimeout(function(){
console.log(testArr.length === testSuccess && testArr.indexOf(false) < 0 ? 'SUCCESS' : 'FAILURE');
}, 200);
// The "jQuery" way.
$.when(dfds[0], dfds[1], dfds[2]).then(test(true), test(false));
$.when(dfds[0], dfds[1], dfds[2]).done(test(true)).fail(test(false));
$.when(dfds[1], dfds[2], dfds[3]).then(test(false), test(true));
$.when(dfds[1], dfds[2], dfds[3]).done(test(false)).fail(test(true));
// Options!
$.whenthen(dfdYay, test(true));
$.whenthen(dfdYay, [test(true), test(true)]);
$.whenthen(dfdYays, [test(true), test(true)]);
$.whenthen(dfdYay, test(true), test(false));
$.whenthen(dfdYays, test(true), [test(false), test(false)]);
$.whenthen(dfdYay, [test(true), test(true)], test(false));
$.whenthen(dfdYays, [test(true), test(true)], [test(false), test(false)]);
$.whenthen(dfdNay, test(false));
$.whenthen(dfdNay, [test(false), test(false)]);
$.whenthen(dfdNays, [test(false), test(false)]);
$.whenthen(dfdNay, test(false), test(true));
$.whenthen(dfdNays, test(false), [test(true), test(true)]);
$.whenthen(dfdNay, [test(false), test(false)], test(true));
$.whenthen(dfdNays, [test(false), test(false)], [test(true), test(true)]);
// 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...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment