Skip to content

Instantly share code, notes, and snippets.

@fearphage

fearphage/README.md

Last active Mar 7, 2020
Embed
What would you like to do?
$.whenAll jQuery extension

$.whenAll is an extension to jQuery's $.when. The main difference is $.when stops when it receives the first rejected promise. This extension treats all successes and failures as progress events. After all the promises have completed, the global promise is resolved if there were no errors. Otherwise the global promise is rejected.

Sameple #1

$.whenAll($.get('http://github.com'), $.get('good luck with this one'))
  .then(
    // success callback
    function(request) {}
    // failure callback - called once at the end
    ,function() {}
    // progress callback - called twice (before the failure callback)
    ,function(data, state, jqXhr) {
      // $.ajax().done/fail so the params are conditional based on a success or failure
      // The second param is consistently the state of the request.
      if (state == 'sucess') {
        // data is the data returned as expected
        // jqXhr is what you expect
      }
      else {
        // data is actually the jqXhr object for failed requests
        // jqXhr is the text of the error "Not Found" in this example
      }
    }
  )
;

Sample #2

var baseUrl = 'http://magic.value/here/'.
$.whenAll($('input.foo')
  .map(function() {
    return $.get(baseUrl + this.value);
  })
  .get()
)
  .progress(function() {
    // update visuals to show this failed
  })
  .failed(function() {
    // reset state to allow the user to resubmit
  })
;
;(function($) {
var slice = [].slice;
$.whenAll = function(array) {
var
resolveValues = arguments.length == 1 && $.isArray(array)
? array
: slice.call(arguments)
,length = resolveValues.length
,remaining = length
,deferred = $.Deferred()
,i = 0
,failed = 0
,rejectContexts = Array(length)
,rejectValues = Array(length)
,resolveContexts = Array(length)
,value
;
function updateFunc (index, contexts, values) {
return function() {
!(values === resolveValues) && failed++;
deferred.notifyWith(
contexts[index] = this
,values[index] = slice.call(arguments)
);
if (!(--remaining)) {
deferred[(!failed ? 'resolve' : 'reject') + 'With'](contexts, values);
}
};
}
for (; i < length; i++) {
if ((value = resolveValues[i]) && $.isFunction(value.promise)) {
value.promise()
.done(updateFunc(i, resolveContexts, resolveValues))
.fail(updateFunc(i, rejectContexts, rejectValues))
;
}
else {
deferred.notifyWith(this, value);
--remaining;
}
}
if (!remaining) {
deferred.resolveWith(resolveContexts, resolveValues);
}
return deferred.promise();
};
})(jQuery);
@nkskalyan

This comment has been minimized.

Copy link

@nkskalyan nkskalyan commented Dec 3, 2014

You saved my day bro 😄

@fearphage

This comment has been minimized.

Copy link
Owner Author

@fearphage fearphage commented Feb 9, 2015

Outstanding. Glad I could help.

@ThumbSnail

This comment has been minimized.

Copy link

@ThumbSnail ThumbSnail commented Sep 24, 2015

Awesome, solved my problem and worked great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment