Skip to content

Instantly share code, notes, and snippets.

@agnel
Created June 9, 2017 09:04
Show Gist options
  • Save agnel/39cd74cb84c0c642189eab2db9b6f1ca to your computer and use it in GitHub Desktop.
Save agnel/39cd74cb84c0c642189eab2db9b6f1ca to your computer and use it in GitHub Desktop.
Multiple Ajax requests using jQuery.when
var requests = [];
var responses = [];
var urls = []; // sample urls
$.each(urls, function(url) {
requests.push( $.get(url, getQueryParams(), function(resp){ responses.push(resp) }) );
});
// here this is the object to which the arguments is to be passed
// so `this` is $.when and the arguments is the `requests` array. it can be even written as $.when.apply( $.when, requests )
$.when.apply( this, requests ).done(function(){
// all the respectives responses for the urls are stored in the responses
// example: responses[0] maps to requests[0]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment