Last active
August 29, 2015 14:00
-
-
Save rmcvey/11056142 to your computer and use it in GitHub Desktop.
counter style ajax transactions
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 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