1

I have a grid with a checkbox filter. When I make changes to that filter a request will pe send to server and get data:

$http({
    url: this.myUrl,
    params: query
});

All good if I make changes at a normal speed, but If I make changes very very fast, lots of requests will be send. In some cases it's possible that last one executes before the last but one.

To avoid getting previous data for last call I want to cancel request if I will do another one.

LIKE:

function makeRequest(){
  var request = $http({
        url: this.myUrl,
        params: query
    });

  request.then(({data})=> //do something with data);
}

function program(){
  on('filterChanged') => {
      abortPreviousPendingRequests();  // how to do that? but only for that url!
      makeRequest();
  }
}

========SOLUTION=========

get logics from here

you need a global variable:

var promise;

function fetchRequest(query) {
                    const canceller = $q.defer();

                    promise = $http({
                        url: that.yourURL,
                        params: query,
                        timeout: canceller.promise
                    });

                    promise.cancel = () => {
                        canceller.resolve();
                    };
            }

before you call fetchRequest(query); you could do this:

function cancelPreviousRequest() {
                    if (!promise) {
                        return;
                    }

                    promise.cancel();
                }

if you will have promise.then(()=>{}).then(() => {}) etc take a look on that link.

Alex
  • 1,013
  • 1
  • 13
  • 27

1 Answers1

0

For solution look in question body :)

Alex
  • 1,013
  • 1
  • 13
  • 27