-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (31 loc) · 911 Bytes
/
index.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
const { debuglog } = require('util');
const debug = debuglog('promise-retry2');
/**
* [promise-retry: catch promise error and retry]
* @param {[type]} task [description]
* @param {[type]} n [description]
* @param {[type]} timeout [description]
* @return {[type]} [description]
* @source https://gist.github.com/song940/6e3e1ec0380956006cd1
*/
module.exports = function retry(task, n, timeout) {
n = n || 1;
timeout = timeout || 0;
var timeouts = [];
if (Array.isArray(n)) {
timeouts = n;
} else {
while (n--) timeouts.push(timeout);
}
return new Promise(function (accept, reject) {
(function fn() {
task(this).then(accept, function (err) {
if (timeouts.length) {
timeout = timeouts.shift();
debug('%ss after retry', timeout / 1000);
setTimeout(fn, timeout);
} else reject(err);
});
})();
});
}