title: AngularJS and an async function inside of a service
body: <p>I am having this issue with an async function. The point is that the "execution" is not waiting for the return of <code>NotificationService.confirm</code> callback.</p>
  
  <p>I mean, that shows a PhoneGap alert, but it's not waiting for evaluate which button pressed the user. So, you can see in the console output <code>undefined</code> instead of false/true/3 values</p>
  
  <p>[EDIT]</p>
  
  <p>This is the code proposed by Maxim Shoutin, but it isn't working yet:</p>
  
  <p><strong>NotificationController.js</strong></p>
  
  <pre><code>angular.module('app').controller("NotificationController", function($rootScope) {
  
      $rootScope.cancel_button = function() {
        var confirm = NotificationService.confirm("Do you want to confirm?", 'Yes!');
  
        confirm.then(function(result) {
          console.log('Confirm: ' + result);
          if(confirm)   $location.path('/menu');
        }, function(result) {
          console.log('No data returned');
        })
      }
  
     /* Additional controller code... */
  }
  </code></pre>
  
  <p><strong>NotificationService.js</strong></p>
  
  <pre><code>angular.module('app').factory("NotificationService", function() {
  
    // Callback function
    var onConfirm = function(button) {
      console.log('Callback function called!!!');
      if(button == 1)       return false;
      else if(button == 2)  return true;
      else if(button == 3)  return 3;
      else                  return false; // dismissed without press button
    };
  
   return {
       confirm : function(alert_msg, title, buttonsArray) {
  
          var deferred = $q.defer();
  
          if(buttonsArray == null) {
               buttonsArray = ['Cancel', 'OK'];
          }
  
           var data = navigator.notification.confirm(
                          alert_msg,      // message
                          onConfirm,      // callback
                          title,          // title
                          buttonsArray    // buttonsArray
                      );
  
           deferred.resolve(data);
           return deferred.promise;
        }
    }
  }
  </code></pre>
  
  <p><strong>CONSOLE OUTPUT</strong></p>
  
  <p><code>&gt; Confirm: undefined</code> (<strong>BEFORE</strong> user pressed the button)</p>
  
  <p><code>&gt; Callback function called!!!</code>  (<strong>AFTER</strong> user pressed the button)</p>