1

Looking at this AuthInterceptor from this helpful answer, how are the request and response key's used in the returned JSON object?

Also, what's the meaning of return config || $q.when(config)? I understand that the second part is returned if config is null or undefined, but what does $q.when(config) mean in this code?

myApp.factory('AuthInterceptor', function ($window, $q) {
    return {
        'request': function(config) {
            config.headers = config.headers || {};
            if ($window.sessionStorage.getItem('token')) {
                config.headers.Authorization = $window.sessionStorage.getItem('token');
            }
            return config || $q.when(config);
        },
        'response': function(response) {
            if(response.status === 401) {
                $location('/login');
            }
            return response || $q.when(response);
        }
    };
});

I typed out the above linked answer. It worked for me, but I don't understand how it's used.

0

2 Answers 2

1

When registering an $http interceptor, you should essentially pass an object with the following (optional) keys: request, requestError, response, responseError.
For more info on when each interceptor function is called, take a look at this answer.

$q.when() turns any JS object into a promise (that resolved immediatelly with the passed in object as each resolved value). But, frankly (although I've seen it a lot) I don't understand why one would need to return config || $q.when(config), instead of just config.

Sign up to request clarification or add additional context in comments.

2 Comments

could you please say a bit more: I don't understand why one would need to return config || $q.when(config), instead of just config.? I'd like to know what you're thinking, please.
Actually, there nothing moe to say other than what I said in my comment to Matt Way's answer (which is essentially what you said in your comment).
1

request and response are simple used as intermediaries. In other words, the request function is run prior to a request (after requesting), and the response function is run prior to handling a request result.


$q.when() ensures that the return value is able to be handled as a promise. Essentially it wraps the value in a promise, so that functions like returnedValue.then() work.

See: https://docs.angularjs.org/api/ng/service/$q

Kevin asked why you would want to use $q.when(). This is to ensure that any outside function that consumes this one receives a promise, if it is expecting one. For example, imagine the situation where you call a function expecting a promise:

someThirdPartyFunc(someArg).then(function(result){
    // do somethign with result
});

If you ran this and someThirdPartyFunc returned a simple value (say 100), then this would obviously fail. If the implementor wrapped the return value in $q.when(100), then the function call above would be valid, and result would contain the value 100.

4 Comments

why would you want to handle a return variable as a promise?
Updated answer for you.
so let's say that my regular, non-intercepting code executes: $http.post("foo/bar.com"), expecting a promise back. This HTTP POST returns an object: {count: 100}. The above response property of my AuthInterceptor would return the object as response is truthy. Wouldn't this implementation then break the expected promise result of the caller of the $http.post(...)? Or perhaps it's up to the implementor to decide whether to return response or response wrapped in a promise?
This is why I don't see the purpose of returning a promise. Normally (i.e. if config is defined) the function will return an object (not a promise), but if config is not defined then it will return a promise (wrapped around the undefined config). It doesn't make sense and indeed it seems like breaking your own implementation (but again I might be missing something).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.