I checked this post and the answer is really good: Wait until all jQuery Ajax requests are done?
But I just want to be more generic: I wonder how we can use this logic for a List of services (ajaxservices) and list of callbacks e.g.
ajaxservices = ["url-getdata1", "url-getdata2"];
callbacks = [callbackdata1, callbackdata2];
callbackdata1 = function (data){...}
$.when(/*somehow call all ajaxservices[]*/).done(function (dataList) {
for (var i = 0; i < callbacks.length; i++) {
callbacks[i](dataList???[i][0]);/* somehow pass the data as parameter*/
}
});
Thanks!
More Info: http://api.jquery.com/jQuery.when/
Do not use
eval
, but theapply
method of functions which takes an array of arguments to call the function with:*Updated - DIDN'T WORK AS I EXPECTED (see real solution below)
Failed Approach
I found a way out using
eval
... it is not fully tested but seems ok. Please feels free to post your comments/trade-off.callAjax
is a method that receive an url and return a promise such asreturn $.ajax(...)
, but since I should executed it using.when
statement I put all the calls in a string to later eval them inside the.when
statement.Since I don't expect more than 10 ajax calls, I hardcoded these 10 responses (arg 0 - 9) as you see below... If I only use 3 ajax services, then only 3 arg should have value and the others will be
undefined
and never will be eval becausecallbacks.length
should be 3 as well.Posible Solution
the call ajax didn't work as I expected so I decided to use another approach. Tested and works fine!
Where ajax is will return 0 if there is not ajax service to call.