Two service calls need to be executed synchronously in a specific order, using promises. However, the execution is not happening as intended.
Controller:
vm.nominationSVC.zipping(vm.fileSelected, vm.selectedCategoryId).
then(function (response: any) { //Zipping
vm.nominationSVC.downloadDocument("documents.zip");
}).
then(function (response: any) {
var deffered = vm.$q.defer();
for (i = 0; i < vm.rowSelectedLength; i++) {
vm.objDownloadHistory.Nomination_Id = vm.nominationIdSelected[i];
vm.objDownloadHistory.FilePath = vm.fileNamesSelected[i];
vm.promises.push(vm.nominationSVC.updateDownloadHistory(vm.objDownloadHistory));
}
// vm.$q.all(vm.promises).then(function () {
// console.log("sdsd");
// });
}).
then(function (response: any) {
vm.getNomiantionList();
});
The method vm.nominationSVC.updateDownloadHistory(vm.objDownloadHistory) does not execute fully and moves down to other .then method, vm.getNomiantionList();
Attempts have been made with $q.all as mentioned in commented code but the issue remains unresolved.
Service Method:
updateDownloadHistory(objDownloadHistory: SpotAward.DownloadHistory)
{
var vm = this;
var url: any;
var deferred = this.$q.defer();
url = this.BaseUrl + 'DownloadHistory/UpdateDownload';
if (url !== null) {
this.$http.post(
url,
JSON.stringify(objDownloadHistory),
{
headers: {
'Content-Type': 'application/json'
}
}
).then(function (result: any) {
if (result.data > 0)
deferred.resolve(result.data);
}).catch((data) => {
deferred.reject(data);
});
}
return deferred.promise;
}