(
predicate: () => boolean | Promise<boolean>,
ms: number,
options: WaitForOptions = {},
)
| 44 | * ``` |
| 45 | */ |
| 46 | export function waitFor( |
| 47 | predicate: () => boolean | Promise<boolean>, |
| 48 | ms: number, |
| 49 | options: WaitForOptions = {}, |
| 50 | ): Promise<void> { |
| 51 | const { step = 100 } = options; |
| 52 | |
| 53 | // Create a new promise that resolves when the predicate is true |
| 54 | let timer: ReturnType<typeof setTimeout>; |
| 55 | const p: Promise<void> = new Promise(function (resolve) { |
| 56 | const setTimer = () => { |
| 57 | timer = setTimeout(async () => { |
| 58 | if (await predicate()) { |
| 59 | resolve(); |
| 60 | } else { |
| 61 | setTimer(); |
| 62 | } |
| 63 | }, step); |
| 64 | }; |
| 65 | setTimer(); |
| 66 | }); |
| 67 | |
| 68 | // Return a deadline promise |
| 69 | return deadline(p, ms, options).finally(() => clearTimeout(timer)); |
| 70 | } |
no test coverage detected