Is there a way to delay the execution of a function until all my resources have resolved successfully? I want to parse through the log array only after all the $resources have resolved and then push a single success notification to the UI instead of multiple notifications for each success.
The code below is inspired by this question on Stack Overflow: angular -- accessing data of multiple http calls - how to resolve the promises. Even though $scope.promises is empty because item.$save() doesn't return anything, you can see that I am trying to add the unresolved promise to the promises array.
$scope.save = function () {
$scope.promises = [];
$scope.log = [];
angular.forEach($scope.menuItems, function(item) {
$scope.promises.push(item.$save(function(menu){
debugger; // execution gets here 2nd
console.debug("success");
$scope.log.push(msg: 'success');
}));
}, this);
$q.all($scope.promises).then(function() {
debugger; // execution gets here 1st
console.debug("all promises resolved");
});
};