All files / src/utils wait-until.ts

86.66% Statements 13/15
60% Branches 3/5
75% Functions 3/4
80% Lines 8/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24            4x 4x 4x           4x 4x 4x 4x 4x          
export interface WaitUntilParams {
  checkCondition: () => boolean
  timeoutMs?: number
  intervalMs?: number
}
 
export function waitUntil({ checkCondition, intervalMs = 250, timeoutMs = 2000 }: WaitUntilParams) {
  return new Promise<void>((resolve, reject) => {
    const timeoutId = setTimeout(() => {
      clearInterval(interval)
 
      reject(new Error('Timeout'))
    }, timeoutMs)
 
    const interval = setInterval(() => {
      if (checkCondition()) {
        clearTimeout(timeoutId)
        clearInterval(interval)
        resolve()
      }
    }, intervalMs)
  })
}