Last active
November 29, 2017 13:05
-
-
Save KhalilZaidoun/73eef6d0cf4f9f454c32299db7e15b7c to your computer and use it in GitHub Desktop.
Function to wait until condition is met before resolving promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function promiseWhen(condition, timeout = 2000) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject(); | |
}, timeout); | |
const loop = () => { | |
if (condition()) { | |
return resolve(); | |
} | |
setTimeout(loop, 0); | |
}; | |
setTimeout(loop, 0); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment