All files waitUntil.ts

100% Statements 17/17
100% Branches 6/6
100% Functions 5/5
100% Lines 15/15

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 25 26 27 28 29 301x   1x   5x   3x 3x 3x       602x 172x   602x 1x 1x   601x       3x 3x       1x  
const MAX_MILLIS = 2000;
 
const waitUntil = (
  predicate: () => boolean,
  errorMessage: string = `Predicate did not become true in ${MAX_MILLIS}ms`
): Promise<void> => {
  let timedOut = false;
  const timeout = setTimeout(() => (timedOut = true), MAX_MILLIS);
  const recursivelyResolve = (
    resolve: () => void,
    reject: (message: string) => void
  ) => {
    if (timedOut) {
      reject(errorMessage);
    }
    if (predicate()) {
      clearTimeout(timeout);
      resolve();
    } else {
      setTimeout(() => recursivelyResolve(resolve, reject), 10);
    }
  };
 
  return new Promise((resolve, reject) => {
    recursivelyResolve(resolve, reject);
  });
};
 
export default waitUntil;