AngularJS中的承诺回报链

时间:2017-05-28 11:08:27

标签: angularjs promise angular-promise

我有以下代码:

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;

我希望startSetup在完成之前的所有内容之后从UserService.getInitialUsersList返回promise。

我的主叫代码是:

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

和`UserService.getInitialUsersList'的代码是:

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;
}

我知道我还没有处理拒绝,但这不是问题所在。当控制权传递回调用代码时,似乎偶尔会UserService.getInitialUsersList完全无法运行。

我是否正确地将promises链返回给调用代码?

由于

0 个答案:

没有答案