Help understand: Service is not launched in controller code but after...?

Hey guys,

Mystery for me…
Why is the service LocalDataService.getCacheUserUpdate(); not launched in it ? Here is some code details.
I modified the code to follow the same pattern as the chatsUpdate but the LocalDataService.getCacheUserUpdate(); gets skipped all the time when I debug step by step… but it is triggered after the function pull refresh is complete

Thanks!

My controller:

// Check for new updates in conversations and return the number of new messages
  var getChatsUpdates = function() {
    DataService.getHasNewMessages().success(function(response) {
      $scope.chatsUpdates = response.chats_updates;
      //console.log($scope.chatsUpdates)
    });
  };
  getChatsUpdates();
  
  var updateUser = function() {
    LocalDataService.getCacheUserUpdate().success(function(response) {
      $scope.user = [];
      $scope.user = DataService.getUser();
    }).error(function() {
      console.log('error on userupdate')
    })
  }
  
  var updateUserFreeStatus = function() {
    updateUser()
    $scope.user_free_status = [];
    $scope.user_free_status = DataService.getUserFreeStatus();
  }
  
  $scope.doHomeRefresh = function() {
    console.log('Start refreshing!')
    updateUserFreeStatus()
    getChatsUpdates()
    $scope.$broadcast('scroll.refreshComplete');
    $scope.$apply()
    console.log('refreshed!')
  };

This is the getChatsUpdates() function defined in the same controller:

// Check for new updates in conversations and return the number of new messages
var getChatsUpdates = function() {
    DataService.getHasNewMessages().success(function(response) {
      $scope.chatsUpdates = response.chats_updates;
      //console.log($scope.chatsUpdates)
    });
  };

And my service:

.factory('LocalDataService', function(WebService, $localstorage) {
    return {
        getCacheUserUpdate: function() {
            var userUpdate = WebService.get('profile/get/user/update');
            
            userUpdate.success(function(response) {
                $localstorage.unset('user')
                console.log(response.user)
                $localstorage.setObject('user', response.user)
            });
            return userUpdate;
        }
        };
})
.factory('DataService', function($q, $localstorage, WebService) {

  
  return {
    getUser: function() {
        return $localstorage.getObject('user');
    },
    // get updates to check if new messages in conversations
    getHasNewMessages: function() {
        var updates = WebService.get('conversations/updates/new-messages');
        updates.success(function(response) {
            // nothing
        });
        updates.error(function(response) {
            console.log('error in get messages: ' + response)
        });
        return updates;
    }    
  };
})

I really don’t get what is happening there…

Anyone? :smile: I feel like I am missing something “easy”