Skip to content

Instantly share code, notes, and snippets.

@tehsis
Created November 6, 2013 18:25
Show Gist options
  • Save tehsis/7341485 to your computer and use it in GitHub Desktop.
Save tehsis/7341485 to your computer and use it in GitHub Desktop.
var foo = ["one", "two", "three", "four"];
foo.forEach(function(value, index) {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/ajax');
// Save the element's index to which this request is
// related to.
xhr.index = index;
xhr.send();
// Since the first element doesn't have a predecessor
// just process it withouth waiting any response.
processElement(foo[0]);
xhr.addEventListener('load', function() {
var next_element_index = this.index + 1;
if (foo[next_element_index] !== undefined) {
processElement(foo[next_element_index], this.response);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment