Hi, I need help with canceling my HTTP request after 2 seconds. If no data is received within that time frame, I want it to resolve into the error function and return an empty object.
I understand that I need to use the timeout property, but I'm not sure where exactly to use the $timeout. Can someone explain it to me in simpler terms?
app
.service('testService',['$http','$q','$timeout',
function($http,$q,$timeout){
var canceler=$q.defer();
this.options = function (long,lat) {
return $http({
method: 'POST',
url: '/coordinates',
headers: {
'Content-Type' : 'application/json; charset=utf-8',
'Data-Type': 'json'
},
data: {
"long":long,
"lat": lat
},
timeout:canceler.promise
}).then(function (response) {
return response.data;
},function(error){
return {};
});
};
}]);