In a typeahead scenario, I am cancelling the previous call, in case it hasn't returned yet and a new call has been made, using the below code:
function($http, $window, $q, $timeout){
var suggestions = [];
var cancel = $q.defer();
return {
getSuggestions : function(inputText){
var deferred =$q.defer();
var promise = deferred.promise;
cancel.resolve('request cancelled');
cancel = $q.defer();
var baseUrl = "http://localhost:8080/PackagingTest/hello";
var params = {
query : inputText,
suggestionsSize : 10
}
$http.get(baseUrl, {params : params, timeout : cancel.promise}).then(
function(response){
suggestions = repsonse;
deferred.resolve(suggestions);
}, function(error){
//check if cancelled and log the error message
if(){
console.log(someErrorMsg);//should log 'request cancelled'
}
console.log(error);
deferred.reject(error);
});
return promise;
}
}
}
This cancels the previous request everytime a new request is made. But i am not able to get the cancelled status message. The $http error handler is called with data: null, status: -1, config: Object, statusText: "", headers: function
where i can't find any property with my text. Am i doing it wrong or is there no way to attach a message when a request is aborted?