I am having an Array that contains objects in AngularJS. Based on the value of a property (snooz
) of these objects I have to call a POST request (getData.sonnzeUpdate()
). After going through each object in the array, finally, I have to call a GET request. My issue is the GET request (inside the function $scope.getTableData
) is executed before getting the response (res
in .then(function(res){}
) of the POST request.
I have tried with angular.forEach(
) and $q
.
Here is my code sample
var notifiedAlarms = [];
var d = new Date();
var checkTine = d.getHours() + "-" + d.getMinutes() + "-" + "00";
angular.forEach(snoozedData, function (snoozed_asset, asset_key) {
if (snoozed_asset.snooz == checkTine) {
var data = {};
snoozed_asset.snooz = '';
data.data = snoozed_asset;
var deferred = $q.defer();
getData.sonnzeUpdate(data).then(function (res) {
if (res.status == '200') {
toastr.info('Alarm with property ' + data.data.actualFailureArea + ' is activated');
// $scope.getTableData(); //donot want to call it here. as same call will for multiple time
notifiedAlarms.push(deferred.promise);
} else {
// console.log('Error in update');
}
});
} else {
// no matching snooz
}
});
$q.all(notifiedAlarms).then($scope.getTableData());