Skip to content

Instantly share code, notes, and snippets.

@jpillora
Last active October 11, 2015 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpillora/3856141 to your computer and use it in GitHub Desktop.
Save jpillora/3856141 to your computer and use it in GitHub Desktop.
jQuery Deferred Serializing Parallelizing Extensions
//each accepts an array of functions that return a promise
$.Deferred.serialize = function(fns) {
if(!$.isArray(fns) || fns.length === 0)
return $.Deferred().resolve().promise();
var pipeline = fns[0](), c = 1, l = fns.length;
for(;c < l;c++)
pipeline = pipeline.pipe(fns[c]);
return pipeline;
}
$.Deferred.parallelize = function(fns) {
if(!$.isArray(fns) || fns.length === 0)
return $.Deferred().resolve().promise();
return $.when.apply(this,_.map(fns, function(fn) { return fn(); }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment