You can try split the API calling logic into a separate function:
function originalFunction() {
for (var index = 0; index < array.length; index++) {
var elements = array[index];
save(elements, index);
}
}
function save(elements, index) {
MyResource.save(elements).$promise.then(function(response){
doSomething(index);
}, function failed(response){
console.log(response);
});
}
The above solution will make it so that you can access the correct index in the API success handler. However, you cannot print the strings as you specified because the API call is an asynchronous operation, while the other code in the for loop runs synchronously. That means that when you print a "outside" the promise statement, you cannot print the corresponding message until AFTER the API call has completed. However, the next "outside" statement does not wait until the previous API call completes before making the next API call. That is why the print statements will not be in the order you specified.
If you absolutely must print out the statements as you described, then you will need to block subsequent API calls from happening until the current API completes, which will make the overall process slower. If that is what you are wanting to do, the following code should work:
function originalFunction(nextInd) {
// default the id of the next call to 0
nextInd = nextId || 0;
// If the index is valid for the current array, call the API method
if (nextInd >= 0 && nextInd < array.length) {
console.log("index outside promise: "+nextInd );
var elements = array[nextInd];
save(elements, nextId);
}
}
function save(elements, index) {
MyResource.save(elements).$promise.then(function(response){
console.log("index inside promise: "+index);
doSomething(index);
// kick off the API call for the next element in the array
originalFunction(index++);
}, function failed(response){
console.log(response);
// kick off the API call for the next element in the array
originalFunction(index++);
});
}
// You can still call the originalFunction without parameters to kick it off
originalFunction();