How use async service into angular httpClient interceptor

前端 未结 7 933
轻奢々
轻奢々 2020-12-09 18:53

Using Angular 4.3.1 and HttpClient, I need to modify the request and response by async service into the HttpInterceptor of httpClient,

Example for modifying the requ

7条回答
  •  佛祖请我去吃肉
    2020-12-09 19:27

    If I get your question right than you can intercept your request using deffer

       
    
    module.factory('myInterceptor', ['$q', 'someAsyncService', function($q, someAsyncService) {  
        var requestInterceptor = {
            request: function(config) {
                var deferred = $q.defer();
                someAsyncService.doAsyncOperation().then(function() {
                    // Asynchronous operation succeeded, modify config accordingly
                    ...
                    deferred.resolve(config);
                }, function() {
                    // Asynchronous operation failed, modify config accordingly
                    ...
                    deferred.resolve(config);
                });
                return deferred.promise;
            }
        };
    
        return requestInterceptor;
    }]);
    module.config(['$httpProvider', function($httpProvider) {  
        $httpProvider.interceptors.push('myInterceptor');
    }]);

提交回复
热议问题