Skip to content

Instantly share code, notes, and snippets.

@technix
Last active March 10, 2024 14:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save technix/a3199b9d9b474f957df757576d22eecd to your computer and use it in GitHub Desktop.
Save technix/a3199b9d9b474f957df757576d22eecd to your computer and use it in GitHub Desktop.
Wait for variable to become true - in promise
window.testVar = null;
function waitForCondition (variable) {
function waitFor(result) {
if (result) {
return result;
}
return new Promise((resolve) => setTimeout(resolve, 100))
.then(() => Promise.resolve(window[variable]))
.then((res) => waitFor(res));
}
return waitFor();
}
waitForCondition('testVar').then((res) => console.log('>>>', res));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment