Created
September 26, 2011 12:11
-
-
Save Tigraine/1242097 to your computer and use it in GitHub Desktop.
wait for multiple ajax requests to finish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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