Skip to content

Instantly share code, notes, and snippets.

@ibejohn818
Last active April 7, 2016 23:09
Show Gist options
  • Select an option

  • Save ibejohn818/3978c42a4dc594bfe8ba9c8fddb32e06 to your computer and use it in GitHub Desktop.

Select an option

Save ibejohn818/3978c42a4dc594bfe8ba9c8fddb32e06 to your computer and use it in GitHub Desktop.
Angular aborting requests
'use strict';
angular.module('testServices',[])
.service('test',[
'$http','$q',
function($http,$q) {
var testing = function(ag) {
console.log(ag);
}
var _userAccountsIndex = false;
var _cancels = {
};
var userAccountsIndex = function() {
if(_cancels.userAccountsIndex) {
_cancels.userAccountsIndex.resolve("CANCELED");
_cancels.userAccountsIndex = $q.defer();
} else {
_cancels.userAccountsIndex = $q.defer();
}
var req = $q.defer();
$http({
method:'GET',
url:'/home/testing-service',
data:{},
timeout:_cancels.userAccountsIndex.promise
}).success(function(res) {
req.resolve(res);
}).error(function(res) {
req.reject(res);
}).finally(function(res) {
delete _cancels.userAccountsIndex;
});
return req.promise;
};
return {
testing:testing,
userAccountsIndex:userAccountsIndex
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment