-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtimeout_promise.js
More file actions
42 lines (32 loc) · 957 Bytes
/
timeout_promise.js
File metadata and controls
42 lines (32 loc) · 957 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
32
33
34
35
36
37
38
39
40
41
42
const { TimeoutError } = require("./errors/custom_errors");
module.exports = TimeoutPromise;
function TimeoutPromise(handler, timeout)
{
return new Promise((resolve, reject) =>
{
const _timeout = timeout ?? 60000;
let _wasSettled = false;
// Introduce a timeout mechanism in case the process hangs for too long
setTimeout(() =>
{
if (_wasSettled === false)
{
reject(new TimeoutError(`Promise timed out`));
_wasSettled = true;
}
}, _timeout);
handler(_resolve, _reject);
function _resolve(returnVal)
{
if (_wasSettled === false)
resolve(returnVal);
_wasSettled = true
}
function _reject(err)
{
if (_wasSettled === false)
reject(err);
_wasSettled = true
}
});
}