Based on this answer I´m currently trying to create a service which returns data even if the request wasn't successful but it timed out.
this.getStatus = function()
{
var timeoutPromise = $timeout(function () {
canceler.resolve();
console.log("Timed out");
},250);
var canceler = $q.defer();
$http.get("data.js", {timeout: canceler.promise} ).success(function(data){
console.log(data);
$timeout.cancel(timeoutPromise);
return data;
});
};
This is how the Service with the $http request is called
var promises = [
firstService.getStatus(),
secondService.getStatus(),
thirdService.getStatus()
];
$q.all(promises)
.then(function (serviceResults) {
//process Data in case of success
console.log("Result First Service: "+ serviceResults[0]);
console.log("Result Second Service: "+ serviceResults[1]);
console.log("Result third Service: "+ serviceResults[2]);
})
.catch(function() {
//process Mock data in case of Failure
});
It should return either the data from the response if the request was successful or some placeholder data in case of a timeout.
So how is it possible to return data if the request times out?
catchto see if your request fails,.catch(function(response){})then process it there or do whatever when it sees the request has failed.