Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@hapi/hoek/lib/wait.js
1126 views
1
'use strict';
2
3
const internals = {
4
maxTimer: 2 ** 31 - 1 // ~25 days
5
};
6
7
8
module.exports = function (timeout, returnValue, options) {
9
10
if (typeof timeout === 'bigint') {
11
timeout = Number(timeout);
12
}
13
14
if (timeout >= Number.MAX_SAFE_INTEGER) { // Thousands of years
15
timeout = Infinity;
16
}
17
18
if (typeof timeout !== 'number' && timeout !== undefined) {
19
throw new TypeError('Timeout must be a number or bigint');
20
}
21
22
return new Promise((resolve) => {
23
24
const _setTimeout = options ? options.setTimeout : setTimeout;
25
26
const activate = () => {
27
28
const time = Math.min(timeout, internals.maxTimer);
29
timeout -= time;
30
_setTimeout(() => (timeout > 0 ? activate() : resolve(returnValue)), time);
31
};
32
33
if (timeout !== Infinity) {
34
activate();
35
}
36
});
37
};
38
39