In the documentation (version 1.1) of AngularJS about interceptors, the interceptor functions all return something like this
return response || $q.when(response);
However, in my app, 'response' is always defined, so $q.when(response) is never executed. So the question is in what situation will the 'response' be undefined and what will
$q.when(response) // == $q.when(null)
do! because response is undefined/null ?
In the documentation (version 1.1) of AngularJS about interceptors, the interceptor functions all return something like this
return response || $q.when(response);
However, in my app, 'response' is always defined, so $q.when(response) is never executed. So the question is in what situation will the 'response' be undefined and what will
$q.when(response) // == $q.when(null)
do! because response is undefined/null ?
Share Improve this question edited Dec 21, 2018 at 11:59 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Nov 4, 2013 at 15:17 Jeanluca ScaljeriJeanluca Scaljeri 29.1k66 gold badges232 silver badges379 bronze badges 2-
What is
response
in your code, does it promise or some data? – Maxim Shoustin Commented Nov 4, 2013 at 15:37 - it is the response from the server. In my interceptor I do things like "response.headers('someProperty)" – Jeanluca Scaljeri Commented Nov 4, 2013 at 15:42
2 Answers
Reset to default 13$q.when(promise)
→promise
$q.when(nonPromise)
→ a newpromise
, that will asynchronously resolve to the given valuenonPromise
.
Lets see what is $q.when
:
$q.when = function (foreignPromise) {
var deferred = $q.defer();
foreignPromise.then(function (data) {
deferred.resolve(data);
$rootScope.$digest();
}, function (reason) {
deferred.reject(reason);
$rootScope.$digest();
});
return deferred.promise;
}
Factory return $q.when(data)
As we can see $q.when
receives promise or nonPromise and wrap it with.
Factory example:
fessmodule.factory('Data', ['$resource','$q', function($resource, $q) {
var data = [
{
"PreAlertInventory": "5.000000",
"SharesInInventory": "3.000000",
"TotalSharesSold": "2.000000",
"TotalMoneySharesSold": "18.000000",
"TotalSharesBought": "0.000000",
"TotalShareCost": "0.000000",
"EstimatedLosses": "0.000000"
}
];
var factory = {
query: function (selectedSubject) {
return $q.when(data);
}
}
return factory;
}]);
Now we can call it from controller:
Data.query()
.then(function (result) {
$scope.data = result;
}, function (result) {
alert("Error: No data returned");
});
Demo Fiddle
Factory returns $q.when(data) || data
From this example we return promise. So lets change it a bit:
Instead return $q.when(data);
we will write:
return $q.when(data) || data;
It will work as well. But not vice versa.
As I understand Angular knows that controller waits from Data
service the promise and above mentioned statement will use 1st off $q.when(data)
.
Demo 2 Fiddle
Factory returns data || $q.when(data)
Now lets call our Data
service by this way:
$scope.data = Data.query();
No promises, the call is sync.
Out factory seems like:
fessmodule.factory('Data', ['$resource','$q', function($resource, $q) {
var data = [
{
"PreAlertInventory": "5.000000",
"SharesInInventory": "3.000000",
"TotalSharesSold": "2.000000",
"TotalMoneySharesSold": "18.000000",
"TotalSharesBought": "0.000000",
"TotalShareCost": "0.000000",
"EstimatedLosses": "0.000000"
}
];
var factory = {
query: function (selectedSubject) {
return data || $q.when(data);
}
}
return factory;
}]);
Demo 3 Fiddle
My Conclusion
The return data || $q.when(data)
means that our service can return single value or promise. But since we know what type of data our service returns , there is no sense in this statement. Or data
or promise
.
The statements:
return config || $q.when(config);
//AND
return response || $q.when(response);
Are shown in V1.1 of the Documentation of interceptors. It was removed from later versions of the documentation. I think it was a misguided attempt to document that interceptor functions can return either a value or a promise.
The statements otherwise make no sense as config
and response
always have a reference.