Skip to content

Instantly share code, notes, and snippets.

@colthreepv
Created September 11, 2015 21:57
Show Gist options
  • Save colthreepv/6ae0f5313571d92974bd to your computer and use it in GitHub Desktop.
Save colthreepv/6ae0f5313571d92974bd to your computer and use it in GitHub Desktop.
angular forever $http retry
'use strict';
var retryForever = true;
var retryTimeout = 5000;
exports = module.exports = function ($http, $q, $timeout) {
function httpRetry () {
var args = arguments;
var httpPromise = $http.apply(null, args);
// in case there is an error
return httpPromise.catch(function (error) {
// re-throw in case is not an http-error or if retry is disabled
if (!error || !error.data || !error.status || !retryForever) throw new Error(error);
// applies to timeout extended parameters: https://docs.angularjs.org/api/ng/service/$timeout
// the 3 $timeout parameters gets concatenated with original httpRetry arguments, using slice as
// `arguments` it's not a real Array
return $timeout.apply(null, [httpRetry, retryTimeout, false].concat(Array.prototype.slice.call(args)));
});
}
this.httpRetry = httpRetry;
};
exports.$inject = ['$http', '$q', '$timeout'];
// example usage
httpRetry({
method: 'GET',
url: '/apiEndpoint',
}).then(function (response) {
resolve(response.data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment