Skip to content

Instantly share code, notes, and snippets.

@Tigraine Tigraine/wait.coffee
Created Sep 26, 2011

Embed
What would you like to do?
wait for multiple ajax requests to finish
waitForAjaxRequestsToComplete = (callback, functions) ->
successes = 0
failures = 0
failedCalls = []
$(functions).each (index, item) ->
item.done ->
successes += 1
item.fail ->
failures += 1
failedCalls.push item
item.always ->
functions.pop item
if functions.length == 0
callback {
successes: successes,
failures: failures,
failed: failedCalls
}
var waitForAjaxRequestsToComplete = function(callback, functions) {
var failedCalls, failures, successes;
successes = 0;
failures = 0;
failedCalls = [];
return $(functions).each(function(index, item) {
item.done(function() {
return successes += 1;
});
item.fail(function() {
failures += 1;
return failedCalls.push(item);
});
return item.always(function() {
functions.pop(item);
if (functions.length === 0) {
return callback({
successes: successes,
failures: failures,
failed: failedCalls
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.