0

I have the following code:

var service = {
   startSetup: function() {
      //If not logged in, redirect to login page
      console.log('starting setup, checking login status');
      return LoginService.loginStatus().then(function(result) {
        var isGrantedPermissionToRoom = result.data.grantedAccess;

        if (!isGrantedPermissionToRoom) {
          $window.location = $window.location + '/login';
        } else {
          console.log('setting up room');
          return service.setupRoom();
        }
      });
    },
    setupRoom: function() {
      return RoomStateService.getRoomState().then(function() {
        return UserService.getInitialUsersList();
      });
    }
  };

return service;

I want startSetup to return the promise from UserService.getInitialUsersList after everything before it is complete.

My calling code is:

SetupService.startSetup().then(function() {
    console.log('Checking connection to room');
    $scope.checkConnectionToRoom(UserService.myUserInfo)
 }

and the code for `UserService.getInitialUsersList' is:

getInitialUsersList: function() {
  console.log('Getting initial users list');
  var deferred = $q.defer();

  service.getUserList().then(function(results) {
    var newUserList = results.data.results;

    updateLocalUserList(newUserList);
    updateMyInformation();
    HandsUpService.processAllUsers(newUserList);
    deferred.resolve();
  });
  return deferred.promise;
}

I'm aware that I'm not handling rejections yet, but this isn't the problem. It seems that very occasionally UserService.getInitialUsersList isn't being run fully/at all when control is passed back to the calling code.

Am I returning the chain of promises correctly to the calling code?

Thanks

Si-N
  • 1,495
  • 1
  • 13
  • 27
  • 1
    Your calling code looks fine. But you indeed should avoid the [deferred antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) in `getInitialUsersList`. – Bergi May 28 '17 at 11:26
  • 1
    Thanks, I thought that my come up. I don't understand what I should be returning from `getInitialUsersList` without constructing my own promise though as I need the work after the `service.getUserList()` to be completed before I return. Everything inside that function is synchronous. – Si-N May 28 '17 at 11:37
  • why in if (!isGrantedPermissionToRoom) not exists return? – mrmr68 May 28 '17 at 12:16
  • why call deferred.resolve() without argument? – mrmr68 May 28 '17 at 12:19
  • to finish many promises synchronously, so after your code nothing work before completion of synchronous code, use q.all. example https://www.martin-brennan.com/using-q-all-to-resolve-multiple-promises/ – changtung May 28 '17 at 12:28
  • @Si-N You should just `return` the return value of the `then(…)` call, like you did in your other methods – Bergi May 28 '17 at 12:41
  • @Bergi - Thanks, that's working great. – Si-N May 28 '17 at 13:04
  • So the problem really was caused by the rejections that you didn't handle? – Bergi May 28 '17 at 14:29
  • No, I'm not getting unhandled rejection errors. I meant I got rid of the explicit promise by returning the `then(...)` instead. I'm not sure the problem is still there as it is very intermittent and if feel like the caller is (sometimes!) regaining control before `getInitialUsersList` is complete. That might be a red herring though. – Si-N May 28 '17 at 20:52

0 Answers0