MCPcopy Create free account
hub / github.com/denoland/std / waitFor

Function waitFor

async/unstable_wait_for.ts:46–70  ·  view source on GitHub ↗
(
  predicate: () => boolean | Promise<boolean>,
  ms: number,
  options: WaitForOptions = {},
)

Source from the content-addressed store, hash-verified

44 * ```
45 */
46export 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}

Callers 1

Calls 2

deadlineFunction · 0.90
setTimerFunction · 0.70

Tested by

no test coverage detected