Cancel earlier http requests in angularjs

When we want data from last http request, data from earlier request (if any) need to be canceled.

// Code goes here

var canceller = $q.defer();
if(earlierRequest && earlierRequest.canceller)
{
  // Cancel earlier requests
  earlierRequest.resolve();
}
var earlierRequest = $http({
  url:'get_async_data.php',
  timeout:canceller.promise
}).then(function(response){
  // Data received
});
earlierRequest.canceller = canceller;

 

Advertisements