-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresolute.js
75 lines (63 loc) · 2.45 KB
/
resolute.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(function() {
var Resolute = function(opt, callback) {
opt = opt || {};
this.operation = opt.operation;
this.maxRetry = opt.maxRetry || 3;
this.callback = callback;
this.delay = opt.delay || 1000;
this.exponentialBackoff = opt.exponentialBackoff || false;
};
Resolute.prototype.run = function(operation) {
var self = this;
var _checkForPromise = function(opt) {
// Do we have a operation?
var operation = (typeof opt === "undefined") ? this.operation : opt;
if (toString.call(operation) === '[object Function]' || typeof operation === 'function') {
return operation;
}
throw new Error("Operation was not a Function: " + operation);
};
var _fn = _checkForPromise.call(self, operation);
var _delay = function(ms) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, ms);
});
};
var _retry = function(retryCount, maxRetry, fn, delay) {
retryCount = retryCount || 1;
var retryMechanism = function(err) {
if (retryCount >= maxRetry) {
throw err;
}
if (self.exponentialBackoff) {
var randomMs = Math.round(Math.random() * 1000) // a jitter
delay = Math.pow(2, retryCount) * 1000 + randomMs
}
return _delay(delay).then(_retry.bind(null, retryCount + 1, maxRetry, fn, delay));
};
if (typeof self.callback == "function") {
self.callback(retryCount, delay);
}
return fn().then(null, retryMechanism);
};
return _retry(1, self.maxRetry, _fn, self.delay);
};
// CommonJS module
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Resolute;
}
exports.Resolute = Resolute;
}
// Register as an anonymous AMD module
if (typeof define === 'function' && define.amd) {
define([], function() {
return Resolute;
});
}
// If there is a window object, that at least has a document property,
// instantiate and define Resolute on the window
if (typeof window === "object" && typeof window.document === "object") {
window.Resolute = Resolute;
}
})();