[addon] add a timeout for resolve method

This commit is contained in:
TheBeastLT
2021-03-14 18:27:37 +01:00
parent 759f25ca2f
commit 7cc93809bd
6 changed files with 28 additions and 6 deletions

22
addon/lib/promises.js Normal file
View File

@@ -0,0 +1,22 @@
/**
* Delay promise
*/
async function delay(duration) {
return new Promise((resolve) => setTimeout(resolve, duration));
}
/**
* Timeout promise after a set time in ms
*/
async function timeout(timeoutMs, promise, message = 'Timed out') {
return Promise.race([
promise,
new Promise(function (resolve, reject) {
setTimeout(function () {
reject(message);
}, timeoutMs);
})
]);
}
module.exports = { delay, timeout };