crowhill
crowhill

Reputation: 2548

Rejected resolve promise not caught

I am working partly from this post and this answer.

I have this route:

.state('barcodeQuery', {
    url:'/barcode',
    templateUrl: templatePath + '_BarcodeQuery.html',
    controller: 'BarcodeQueryCtrl',
    controllerAs: 'vm',
    requireADLogin: true,
    resolve: {
        user: 'user',
        access: function(user){return user.isAdmin().$promise}
    }
})

user.isAdmin looks like this:

isAdmin: function() {
    return this.getProfile().then(function(data){
        // then return the response.
        if(data.isadmin) {
            return true;
        }
        else {
            return $q.reject(false);
        }
    });
},

So far as I can tell, everything works to this point. isAdmin() does return the proper response, depending on the user.

Things start to go wrong here:

$rootScope.$on("$stateChangeError", function (event, current, previous, rejection) {
    console.log("Is this thing on?");
});

If .isAdmin() rejects the promise, there should be a $stateChangeEror, right? But in my code, that doesn't happen. The console.log("Is this thing on?") line is never reached. No errors, or anything. The page just renders like normal.

I suspect I've been fouled up in a promise somewhere (that's always where I get fouled up), but I don't see it.

Any help? Thanks.

Upvotes: 0

Views: 35

Answers (1)

crowhill
crowhill

Reputation: 2548

I found the problem:

access: function(user){return user.isAdmin().$promise}

Should have been this:

access: function(user){return user.isAdmin()}

The post I linked above claimed that I needed to have $promise to make sure the resolve waited for the function to return it's promise, but it appears this isn't the case.

Upvotes: 1

Related Questions