Skip to content

Instantly share code, notes, and snippets.

@lucabertolasi
Created March 4, 2018 20:23
Show Gist options
  • Save lucabertolasi/e8d016f690bd072dc1e5922c02e53cec to your computer and use it in GitHub Desktop.
Save lucabertolasi/e8d016f690bd072dc1e5922c02e53cec to your computer and use it in GitHub Desktop.
[JavaScript] [AngularJS] Interceptor to allow for cancelling requests.
/**
* @license
* Copyright (c) 2017-present, Luca Bertolasi. All Rights Reserved.
*
* This software is licensed under the terms of the MIT license,
* a copy of which can be found at https://opensource.org/licenses/MIT
*/
// To cancel the request, iterate over '$http.pendingRequests', find the nth request and call '$http.pendingRequests[nth].cancel.resolve()'
(() => {
angular
.module('HttpInterceptor')
.factory('HttpInterceptor', HttpInterceptor);
/* @ngInject */
function HttpInterceptor($q) {
return {
request
}
function request(config) {
if (!config.timeout) {
config.cancel = $q.defer();
config.timeout = config.cancel.promise;
}
// config.withCredentials = true;
return config;
} // request
} // HttpInterceptor
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment