[scraper] adds timeout to seeders updating

This commit is contained in:
TheBeastLT
2020-09-17 21:28:47 +02:00
parent ea9a22ce63
commit 2debc4ee8c
2 changed files with 21 additions and 5 deletions

View File

@@ -33,6 +33,20 @@ 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);
})
]);
}
/**
* Return most common value from given array.
*/
@@ -40,4 +54,4 @@ function mostCommonValue(array) {
return array.sort((a, b) => array.filter(v => v === a).length - array.filter(v => v === b).length).pop();
}
module.exports = { sequence, first, delay, mostCommonValue };
module.exports = { sequence, first, delay, timeout, mostCommonValue };