Skip to content

Instantly share code, notes, and snippets.

@arjenvanderende
Last active December 15, 2015 13:49
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 arjenvanderende/5270255 to your computer and use it in GitHub Desktop.
Save arjenvanderende/5270255 to your computer and use it in GitHub Desktop.
Adds helper method $.any() for promises to jQuery. Usage: $.any(promise1, promise2).then(someAction);
(function( $ ) {
$.any = function () {
var failed = 0;
var count = arguments.length;
var dfd = $.Deferred();
for (var i = 0; i < count; i++) {
arguments[i].then(function(value) {
dfd.resolve(value);
}, function() {
failed++;
if (failed == count) {
dfd.reject();
}
});
}
return dfd.promise();
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment