Created
June 20, 2021 10:37
-
-
Save hmmhmmhm/1b48d2a0203d259e37b764a01616c8cf to your computer and use it in GitHub Desktop.
await when condition end (typescript)
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 const when = ( | |
| condition: () => boolean | Promise<boolean>, | |
| timeoutMs = 0, | |
| repeatMs = 500 | |
| ) => { | |
| return new Promise<void>((resolve) => { | |
| const startTime = Date.now() | |
| const checkFlag = async () => { | |
| if (await condition()) { | |
| resolve() | |
| } else if (timeoutMs !== 0 && Date.now() > startTime + timeoutMs) { | |
| resolve() | |
| } else { | |
| window.setTimeout(checkFlag, repeatMs) | |
| } | |
| } | |
| checkFlag() | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment