我有两个不同的循环,分别执行POST和DELETE操作。 一旦完成所有操作,我想提出另一个服务请求。
我的问题是: 如果删除操作出错,控件将进入catch块并需要一个对话框确认来确定是否仍需要删除该项目。
如果来自对话框的确认,我想永久删除id并且正在发生。但不知怎的,刷新比我想要的更早。我想念一些基本的东西。
function submitAndRefresh(){
var deletePromisesTasks = [];
var addPromisesTasks = [];
angular.forEach(toBeDeleted, function(value,key){
deletePromisesTasks.push(delete(value.id));
});
angular.forEach(toBeAdded, function(value,key){
addPromisesTasks.push(add(value));
});
//To be performed after all the add/delete operations are completely done!
$q.all(deletePromisesTasks,addPromisesTasks).then(function(){
refreshData(); //Another Service Call
});
}
function delete(id, purge_flag) {
var defer = $q.defer();
deleteOrPurge(id, purge_flag).then((promise1) => {
defer.resolve();
}).catch(function(error) {
confirmAndDeleteContractParameters(error, delete, id, true);
}).finally(function(){
return defer.promise;
});
}
function deleteOrPurge(id, purge_flag){
return Service.items().delete({
id: id,
purge_flag: purge_flag || false
}).$promise;
}
function confirmAndDelete(error, callback, parameterId, purge_flag) {
var confirmModal = {
header: 'Confirm',
showCancelButton: true
};
try {
confirmModal.body = 'Some msg. Do you want to continue?';
} catch (e) {}
ModalService.confirmDialog($scope, confirmModal, function(confirm){
if (confirm) {
callback(parameterId, purge_flag);
}
});
}
答案 0 :(得分:0)
作为一项基本规则,当您使用var defer = $q.defer();
启动函数时,函数的最后一行通常应为return defer.promise