-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromise.js
117 lines (104 loc) · 2.86 KB
/
promise.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
function parseRetryOptions (input = {}) {
if (input.parsed) {
return input;
}
let options = input;
if (typeof options === 'number') {
if (options < 1000) {
options = { count: options };
} else {
options = { timeout: options };
}
} else {
options = { ... input };
}
if (typeof options.count !== 'number') {
options.count = -1;
}
if (typeof options.delay !== 'number') {
options.delay = 100;
}
if (typeof options.timeout !== 'number') {
if (options.count > 0) {
options.timeout = 0;
} else {
options.timeout = 30000;
}
}
options.parsed = true;
return options;
}
function delay (timeout = 1000) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(timeout);
}, timeout);
});
}
const validation = {
fail (reason) {
return Promise.reject(reason ? reason : 'validation failed');
},
ok (value) {
return Promise.resolve(value ? value : true);
},
};
function timeoutPromise (promise, options, value, context) {
return new Promise((resolve, reject) => {
try {
promise.bind(context)(value).then((result) => {
resolve(result);
}, (error) => {
options.lastError = error;
setTimeout(() => {
reject(error);
}, options.delay);
});
} catch (error) {
options.lastError = error;
setTimeout(() => {
reject(error);
}, options.delay);
}
});
}
const retry = function (promise, input, value, context) {
const options = parseRetryOptions(input);
options.start ||= Date.now();
if (typeof options.counter !== 'number') {
options.counter = -1;
}
let errorMessage;
if (options.timeout > 0 && Date.now() > options.start + options.timeout) {
errorMessage = `timeout of ${ options.timeout } ms exceeded (${ options.counter } attempts).`;
}
if (options.count > 0 && options.counter >= options.count - 1) {
errorMessage = `retry count of ${ options.count } exceeded.`;
}
if (errorMessage) {
const error = new Error(errorMessage);
if (options.lastError) {
error.stack = options.lastError.stack;
}
throw error;
}
options.counter++;
return timeoutPromise(promise, options, value, context).
then((result) => result, (error) => {
if (error && (error.name === 'ReferenceError' || error.name === 'SyntaxError' ||
error.name === 'TypeError' || error.name === 'RangeError' ||
error.name === 'EvalError' || error.name === 'InternalError' ||
error.name === 'URIError' || error.name === 'UnhandledUrlParameterError' ||
error.name === 'RetryError' || error.group && error.group === 'RetryError')) {
throw error;
} else {
return retry(promise, options, value, context);
}
});
};
module.exports = {
delay,
retry,
validation,
};