Within the HTML, I have a function that is triggered by an ng-click
:
vm.items = [];
vm.moveItems = function() {
angular.forEach(vm.items,
function (item) {
$http({
method: 'PUT',
url: '/api/item_move/' + item.id}
}).then(function(response) {
Toast.success("Successfully moved item " + item.id);
vm.reload();
}, function (error) {
Toast.error("Failed to move item " + item.id, error);
});
});
};
The issue at hand is that the vm.reload()
function is being called after each successful response. Ideally, it should be executed only once after the entire forEach
loop has finished processing. As I am new to asynchronous programming in JavaScript, I would like to learn about the most commonly used methods to address this problem.