-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsleeper.js
More file actions
31 lines (29 loc) · 959 Bytes
/
Copy pathsleeper.js
File metadata and controls
31 lines (29 loc) · 959 Bytes
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
'use strict';
async function sleeper(millisecondsMin, millisecondsMax) {
let timeoutMs;
if ((millisecondsMax === undefined) || (millisecondsMax === null)) {
millisecondsMax = millisecondsMin;
}
if (! (Number.isSafeInteger(millisecondsMin) &&
(millisecondsMin >= 0) &&
(millisecondsMin < 100000000000000))) {
throw new Error('Illegal timeout minimum');
}
if (! (Number.isSafeInteger(millisecondsMax) &&
(millisecondsMax >= 0) &&
(millisecondsMax < 100000000000000))) {
throw new Error('Illegal timeout maximum');
}
if (millisecondsMax < millisecondsMin) {
throw new Error('Illegal timeout range');
}
if (millisecondsMin == millisecondsMax) {
timeoutMs = millisecondsMin;
} else {
timeoutMs = millisecondsMin + Math.floor(Math.random() * (millisecondsMax - millisecondsMin + 1));
}
return new Promise(function(resolve, reject) {
setTimeout(function() { resolve(); }, timeoutMs);
});
}
module.exports = sleeper;