-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretryrequest.factory.js
44 lines (35 loc) · 1022 Bytes
/
retryrequest.factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(function() {
'use strict';
angular
.module('app')
.factory('RetryRequestFactory', RetryRequestFactory);
RetryRequestFactory.$inject = ['$timeout'];
function RetryRequestFactory($timeout) {
var self = this;
self.retryCount = 3;
return {
Go : Go
};
function Go(run, callback) {
retryFunction(run, callback);
}
function retryFunction(run, callback) {
var promise = run();
promise.then(function(data) {
callback(null, data);
}).catch(function(err) {
if(self.retryCount > 0) {
console.log('Wait for 2 seconds ' + new Date());
$timeout(function() {
console.log('Fired after 2 seconds ' + new Date());
self.retryCount -= 1;
retryFunction(run, callback);
}, 2000);
}else {
self.retryCount = 3;
callback(err);
}
});
}
}
})();