After reading this response, I am currently in the process of developing a service that can provide data even if the request fails due to timing 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
});
The objective is to retrieve the actual response data if the request succeeds or display mock data if there is a timeout.
How can data be returned when a request times out?