Created
March 4, 2018 20:23
-
-
Save lucabertolasi/e8d016f690bd072dc1e5922c02e53cec to your computer and use it in GitHub Desktop.
[JavaScript] [AngularJS] Interceptor to allow for cancelling requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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