angular defered resolve undefined

时间:2017-03-22 18:00:42

标签: javascript angularjs angular-promise angular-http

角色的东西让我发疯! tha app使用Bearer身份验证,有像这样的errorInterceptor

   function errorInterceptor($injector, $log, $location) {
        var inFlight = null;
        var authInterceptorServiceFactory = {};
        var _request = function (config) {
            config.headers = config.headers || {};    
            var oauthToken = $injector.get('oauthService').getAuthorizationHeader();
            if (oauthToken) {
                config.headers.Authorization = oauthToken;    
            }
            return config;
        }

        var _responseError = function (rejection) {     
            debugger        
            var deferred = $injector.get('$q').defer();
            switch(rejection.status){
                case 401:
                    console.log('401');
                    if(inFlight == null){
                        var authService = $injector.get('oauthService');
                        inFlight = authService.refreshToken() //this is just a $http call 
                    }
                    inFlight
                        .then(function (response) {
                            _retryHttpRequest(rejection.config, deferred)
                                .success(function (result) {
                                    deferred.resolve(result);
                                })
                                .error(function(err, status) {
                                    deferred.reject(err);
                                })
                                .finally(function() {                                       
                                    inFlight = null;
                                });
                            $injector.get('oauthService').setLocalStorageData(response.data);

                        },
                        function (err, status) {
                                $injector.get('oauthService').logOut();
                        });
                    break;

            } 

            return deferred.promise;
        }

        var _retryHttpRequest = function (config, deferred) {
            var $http = $http || $injector.get('$http');
            return $http(config);
        }

        authInterceptorServiceFactory.request = _request;
        authInterceptorServiceFactory.responseError = _responseError;

        return authInterceptorServiceFactory;
    }

它几乎可以工作,第一个401响应发出“refreshToken”请求,然后所有后续请求都与新令牌一起重新发送。 我面临的问题是在行

deferred.resolve(result);

虽然结果是预期的对象,但是当调用promise函数时,它的参数是未定义的!

承诺函数

sidebarService.getMenu()
        .success(sidebarReady)
        .error(sidebarReadyError);

function sidebarReady(items) { 
        //when errorInterceptor catches the error 
        //and re-sends, when it resolves this funciton, argument is undefined 
        // 
    }

有人可以帮忙吗? 谢谢

2 个答案:

答案 0 :(得分:1)

只需使用.then()而不是.success(),它自Angular 1.5以来已被弃用,并在1.6中删除。使用.success()承诺链接是不可能的,因为它不会返回新的解决承诺。

答案 1 :(得分:0)

事实证明使用.then()代替.success()有效! 谢谢@Nekudotayim

还要感谢@Ben和@Nikolay

:)