I see three separate issues here.
- Your loop index is wrong when the ajax call finishes because the
for
loop has run its course before the async ajax call finishes.
- You will need to find a way to wait until the last async operation is done before appending the "done". You can properly use promises for that.
- Your ajax results will not necessarily be appended in order because it is indeterminate what order the ajax calls will finish in. Depending upon the server architecture and what is involved in the requests, they "may" finish in the order they are requested, but since you are running them all in parallel, that is certainly not guaranteed.
The first issue is because the operation is asynchronous and thus your loop index is at the final value by the time the async operation finishes. See this answer for a solution to that part of the problem. See that solution implemented with your code here: http://plnkr.co/edit/cUFGZL0Bm6unRca3X7N5?p=preview
If you want to know when all the ajax operation are done so you can append the "done", then you will want to properly use promises for that. You can do this:
<script>
array1 = ["helloworld.html", "helloworld.html", "helloworld.html"]
loadData(array1).then(function() {
$('pre').append("done");
}, function() {
// handle error here
});
function loadData(gim) {
var promises = [];
for (var i = 0; i < gim.length; i++) {
(function(index) {
console.log(index);
promises.push($.get(gim[index]).then(function(data) {
console.log("a = " + index);
$('pre').append(data+" -- "+index+"<br>");
}));
})(i);
}
return Promise.all(promises);
}
</script>
Working example: http://plnkr.co/edit/KrC7Lxu9OHs9suvNOC8t?p=preview
Here you collect an array of promises that are returned by $.get()
into an array and you use Promise.all()
to tell you when all those promises are done.
FYI, this code does NOT guarantee that the results will be appended in order as the async operations could complete in any order.
If you want to run the operations in parallel, but make sure they are added in the requested order, then you can change your code like this to collect all the results, then add the results to the DOM in guaranteed order:
<script>
array1 = ["helloworld.html", "helloworld.html", "helloworld.html"]
loadData(array1).then(function() {
$('pre').append("done");
}, function() {
// handle error here
});
function loadData(gim) {
var promises = [];
for (var i = 0; i < gim.length; i++) {
promises.push($.get(gim[i]));
}
return Promise.all(promises).then(function(results) {
results.forEach(function(item, index) {
$('pre').append(item + " -- "+index+"<br>");
});
});
}
</script>
Working example: http://plnkr.co/edit/nUHwFMg22V8rc9v12SqR?p=preview
If you want to use only jQuery promise functions (which are a bit non-standard), you can do this:
<script>
array1 = ["helloworld.html", "helloworld.html", "helloworld.html"]
loadData(array1).then(function() {
$('pre').append("done");
}, function() {
// handle error here
});
function loadData(gim) {
var promises = [];
for (var i = 0; i < gim.length; i++) {
promises.push($.get(gim[i]));
}
return $.when.apply($, promises).then(function() {
var results = Array.prototype.slice.call(arguments);
console.log(results);
results.forEach(function(item, index) {
$('pre').append(item[0] + " -- "+index+"<br>");
});
});
}
</script>
Working example: http://plnkr.co/edit/IHMnEbYEFLjfguDkoubg?p=preview