Skip to content

Instantly share code, notes, and snippets.

@rmcvey
Last active August 29, 2015 14:00
Show Gist options
  • Save rmcvey/11056142 to your computer and use it in GitHub Desktop.
Save rmcvey/11056142 to your computer and use it in GitHub Desktop.
counter style ajax transactions
var endpoints = ['/data/step1', '/data/step2', '/data/step3'];
var run_cnt = 0;
var success_cnt = 0;
function final_action(){
console.log('completed!');
}
function fail_action(){
console.log("something went wrong!");
}
$.each(endpoints, function(index, url){
$.get(url, function(response){
// keep track of requests made
run_cnt++;
// keep track of successful requests
success_cnt++;
// this sucks
if(success_cnt === endpoints.length) {
final_action();
} else if (run_cnt === endpoints.lenght) {
fail_action();
}
}, function(){
run_cnt++;
// duplicate logic
if (run_cnt === endpoints.lenght) {
fail_action();
}
// if our call errors we don't update the counter
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment