AngularJS:通过 Promise,但无法读取未定义的 属性 'finally'

AngularJS: Passing Promise, yet Cannot read property 'finally' of undefined

在我的 angular 应用程序中,我有 2 个方法 save()saveTriggers()saveTriggers() 通过调用 Web 服务 (C#) 更新所有记录。我想确保在 saveTriggers() 中更新所有记录并将控制权返回给 save() 之后执行一段代码。我相信我需要从 saveTriggers() 传递一些东西来执行 finally 块。我尝试了各种各样的事情,没有任何效果。使用 .then() 也会出现同样的错误。我不太擅长 JS。你能指导我吗?

           vm.updatedTriggers = []; // IDs are pushed in
           vm.saveTriggers = function () {
                if (vm.updatedTriggers.length === 0) {
                    vm.close();
                } else {
                    vm.saving = true;
                    vm.save()
                        .finally(function () {    // ERROR - Cannot read property 'finally' of undefined
                            console.log("Saved all. Closing...");   // Never REACHES here
                            vm.saving = false;
                            vm.updated = true;
                            $uibModalInstance.close(true);
                    });                            
                }
            };

            vm.save = function () {
                //vm.saving = true;
                for (var i = 0; i < vm.updatedTriggers.length; i++) {
                    var trigger = vm.triggers.find(t => t.id === vm.updatedTriggers[i]);

                    var input = {
                        id: trigger.id,
                        target: trigger.target,
                        targetInfo: vm.targetData,
                        event: trigger.event,
                        eventQuantity: trigger.eventQuantity,
                        eventQuantityExtra: trigger.eventQuantityExtra
                    };

                    rpmService.editDeviceTrigger(input);
                        /*.finally(function () {                                
                            console.log("Updated event");  // Reaches here
                            vm.updated = true;
                            return Promise.resolve(2);                    
                        });*/ // Commenting this also doesn't help
                }
                return Promise.resolve(2);
            };

rpmService.editDeviceTrigger(输入)

       public async Task EditDeviceTrigger(EditDeviceTriggerInput input) {
               // calls other methods with await
               // Doesn't return anything
       }

编辑:更新代码: 我摆脱了错误,但输出不是预期的系列。

       vm.saveTriggers = function () {
            vm.saving = true;
                   
            vm.save().then
               (function success() {
                    console.log("Returned Result ");
                    console.log("Saved all. Closing...");  // These lines are executed before the event is upated
                    vm.saving = false;                      
                    $uibModalInstance.close(true);
               });
        };

        vm.save = function () {
            var deferred = $q.defer();

            for (var i = 0; i < vm.updatedTriggers.length; i++) {
                var trigger = vm.triggers.find(t => t.id === vm.updatedTriggers[i]);

                var input = {
                    id: trigger.id,
                    ....
                };

                rpmService.editDeviceTrigger(input)
                    .finally(function () {                                
                        console.log("Updated event"); // Successfully updates all events
                        vm.updated = true;
                   });
            }
                                    
            deferred.resolve();
            return deferred.promise;
        };

输出:

 Returned Result 
 Saved all. Closing...
 Updated event

预期输出:

 Updated event
 Returned Result 
 Saved all. Closing...

谢谢。

通常你不需要 $q.defer 相关的东西,但如果你愿意,你可以使用它来做同样的事情。 在这里我猜你只需要收集你所有的保存承诺和 return 使用 $q.all:

的新结果
vm.save = function () {
    const myAwesomePromises = []

    for (var i = 0; i < vm.updatedTriggers.length; i++) {
        ...

        const savePromise = rpmService.editDeviceTrigger(input);
        savePromise.finally(() => console.log('edit device finally'));// <-- not sure u need this
        myAwesomePromises.push(savePromise);
    }
                            
    return $q.all(myAwesomePromises).finally(() => console.log('All edit device finally'));
};