I've stumbled on this several times, and cannot seem to find a good solution:
for (var i = types.length - 1; i >= 0; i--) {
    api.get("test").then(function(res){
        // do something with i here
        $scope.var[i] = res;
    });
};
When the then() function is triggered the iteration is already done, resulting only $scope.var[0] is being used, instead of $scope.var[1], $scope.var[2] etc.
All I can think of is either giving the i variable as argument to the function and return it with the deferred or using callbacks, but neither is really nice. Is there a solution for this?
 
    