0

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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
gaurav5430
  • 12,934
  • 6
  • 54
  • 111

1 Answers1

0

There's actually no way of passing data to catch handler on abort. Below is the onabort callback, that angular uses internally:

  var requestError = function() {
    // The response is always empty
    // See https://xhr.spec.whatwg.org/#request-error-steps and https://fetch.spec.whatwg.org/#concept-network-error
    completeRequest(callback, -1, null, null, '');
  };

Where the definition of completeRequest is:

function completeRequest(callback, status, response, headersString, statusText) {...}
xReeQz
  • 320
  • 1
  • 7