Created
June 9, 2017 09:04
-
-
Save agnel/39cd74cb84c0c642189eab2db9b6f1ca to your computer and use it in GitHub Desktop.
Multiple Ajax requests using jQuery.when
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 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