1
votes

We are all using interceptors for our applications. But since i used it, i don't understand the retunr statement of the request & response method inside the interceptor :

return config || $q.when(config);

As i understand it, if the config object is undefined the method will return the config wrap in the when method of promise API because we don't know if it's a promise or not. That's ok i think.

But my question is how is it possible to get an undefined config Object in a request ?

Thx for your replies

2

2 Answers

0
votes

First of all, I don't think the public API allows you to pass in a falsy config. You can call $http(null) but this will throw TypeError: Cannot read property 'headers' of null.

But then I don't understand the purpose of the || when either. If $q.when(config) is called, you know config is falsy. So the interceptor returns a promise that resolves to null, for example. Why not just return null ?

0
votes

From the docs:

The function is free to modify the config or create a new one. The function needs to return the config directly or as a promise

If interceptor 1 returns undefined, then interceptor 2 would get an undefined config.

But that doesn't explain the statement, because we still wouldn't have a valid config (with headers etc.). So it's not meant as "real" code but is rather the statement from the docs:

return the config directly = return config or as a promise = || $q.when(config)