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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s