title: How to use Promise in Service AngularJS? Sample Data Firebase
body: <p>Here is my service.js file: </p>
  
  <pre><code>    angular.module('starter.services', [])
      .factory('firebaseData', function() {
         return {
            refUserFacebook: function (userUid) {
              var firebaseRef =  new Firebase("https://sweltering-heat-772.firebaseio.com/users/");
              var user;
              firebaseRef.once('value', function(dataSnapshot) {
                  var users = dataSnapshot.val();
                  var userKeys = Object.keys(users);
                  for (var i = 0, len = userKeys.length; i &lt; len; i ++){
                      if (users[userKeys[i]].uid == userUid){
                          user = users[userKeys[i]];
                      }
                  }
                  console.log(user);
                  return user;
              });
            }
          }
       })
  </code></pre>
  
  <p>Here is my controller file name item.js file</p>
  
  <pre><code>angular.module('starter.controllers.item', [])
  
  .controller('ItemCtrl', function($scope, firebaseData) {
  
      var ref = firebaseData.ref();
      var userUid = ref.getAuth();
      var user = firebaseData.refUserFacebook(userUid.uid);
      console.log(user);
  
  })
  </code></pre>
  
  <p>Of course I'll get undefined in the item.js controller first, and user in service return after. </p>
  
  <p>I'd really like to use Promise technology in my controller, to return user value from the service. </p>
  
  <p>In item.js file, it would be something like:</p>
  
  <pre><code>firebaseData.refUserFacebook(userUid.uid).then(function(user){
     console.log(user); //Promise after retrieving value from service.js
  });
  </code></pre>
  
  <p>Any helps would be appreciated. </p>
  
  <p>Thanks</p>