If you want to execute the Ajax calls sequentially, you have to return the promise from the callback and also attach a new callback to the last promise object:
var dfd = $.Deferred(),
promise = dfd.promise(),
i = 0,
c = 0;
while (i <= qty_of_gets) {
// needs an IIFE
(function(i)
promise = promise.then(function(){
return $.get("queries/html/" + product_id + i + ".php");
});
}(i++));
}
promise.done(function(){
while (c <= qty_of_gets){
$('myDiv').append(c);
c++;
}
});
// resolve deferred
dfd.resolve();
As of jQuery 1.8, you should use .then
instead of .pipe
.
Another problems is (in your example at least) that at the time the callbacks are executed, i
won't have the value you expect. You can use an immediately invoked function expression to capture the current value of i
. See JavaScript closure inside loops – simple practical example for more info.
There is no clean solution for getting the results. I think the best you could do is adding the results to an array and access that array in the .done
callback. I.e.:
var results = [];
while (i <= qty_of_gets) {
// needs an IIFE
(function(i)
promise = promise.then(function(){
return $.get("queries/html/" + product_id + i + ".php")
.then(function(result) {
results[i] = result;
});
});
}(i++));
}
promise.done(function(){
// do something with `results`
});