Improved await function.

This commit is contained in:
Ajay Ramachandran
2019-08-23 21:10:28 -04:00
parent c1efe02614
commit 2d12f957bc

View File

@@ -1,14 +1,20 @@
// Function that can be used to wait for a condition before returning // Function that can be used to wait for a condition before returning
async function wait(condition, timeout = 5000, check = 100) { async function wait(condition, timeout = 5000, check = 100) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
setTimeout(() => {reject("TIMEOUT")}, timeout); setTimeout(() => reject("TIMEOUT"), timeout);
const interval = setInterval(() => {
let intervalCheck = () => {
let result = condition(); let result = condition();
if (result !== false) { if (result !== false) {
resolve(result); resolve(result);
clearInterval(interval); clearInterval(interval);
}; };
}, check); };
let interval = setInterval(intervalCheck, check);
//run the check once first, this speeds it up a lot
intervalCheck();
}); });
} }
@@ -37,5 +43,6 @@ function getYouTubeVideoID(url) {
return false; return false;
} }
} }
return false; return false;
} }