title: AngularJS: Passing Promise, yet Cannot read property 'finally' of undefined
body: <p>In my angular app, I have 2 methods <code>save()</code> and <code>saveTriggers()</code>. <code>saveTriggers()</code> updates all records by calling a web service (C#). I want to make sure that a block of code is executed after all records are updated in <code>saveTriggers()</code> and control is returned to <code>save()</code>. I believe I need to pass something from the <code>saveTriggers()</code> to make finally block execute. I tried various things, nothing works. Using .then() also gives the same error. I am not that good at JS. Can you please guide me.</p>
  <pre><code>           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(&quot;Saved all. Closing...&quot;);   // Never REACHES here
                              vm.saving = false;
                              vm.updated = true;
                              $uibModalInstance.close(true);
                      });                            
                  }
              };
  
              vm.save = function () {
                  //vm.saving = true;
                  for (var i = 0; i &lt; vm.updatedTriggers.length; i++) {
                      var trigger = vm.triggers.find(t =&gt; 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(&quot;Updated event&quot;);  // Reaches here
                              vm.updated = true;
                              return Promise.resolve(2);                    
                          });*/ // Commenting this also doesn't help
                  }
                  return Promise.resolve(2);
              };
  </code></pre>
  <p>rpmService.editDeviceTrigger(input)</p>
  <pre><code>       public async Task EditDeviceTrigger(EditDeviceTriggerInput input) {
                 // calls other methods with await
                 // Doesn't return anything
         }
  </code></pre>
  <p><strong>EDIT: Updated Code:</strong> I got rid of the error, but the output is not is expected series.</p>
  <pre><code>       vm.saveTriggers = function () {
              vm.saving = true;
                     
              vm.save().then
                 (function success() {
                      console.log(&quot;Returned Result &quot;);
                      console.log(&quot;Saved all. Closing...&quot;);  // 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 &lt; vm.updatedTriggers.length; i++) {
                  var trigger = vm.triggers.find(t =&gt; t.id === vm.updatedTriggers[i]);
  
                  var input = {
                      id: trigger.id,
                      ....
                  };
  
                  rpmService.editDeviceTrigger(input)
                      .finally(function () {                                
                          console.log(&quot;Updated event&quot;); // Successfully updates all events
                          vm.updated = true;
                     });
              }
                                      
              deferred.resolve();
              return deferred.promise;
          };
  </code></pre>
  <p><strong>OUTPUT:</strong></p>
  <pre><code> Returned Result 
   Saved all. Closing...
   Updated event
  </code></pre>
  <p><strong>EXPECTED OUTPUT:</strong></p>
  <pre><code> Updated event
   Returned Result 
   Saved all. Closing...
  </code></pre>
  <p>Thanks.</p>