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

Function poll

async/poll.ts:69–95  ·  view source on GitHub ↗
(
  fn: () => T,
  isDone: (result: Awaited<T>) => boolean,
  options: PollOptions = {},
)

Source from the content-addressed store, hash-verified

67 * @returns The result of `fn` when `isDone` returns `true`.
68 */
69export async function poll<T>(
70 fn: () => T,
71 isDone: (result: Awaited<T>) => boolean,
72 options: PollOptions = {},
73): Promise<Awaited<T>> {
74 const { signal, interval = 1000 } = options;
75
76 if (!Number.isFinite(interval) || interval < 0) {
77 throw new RangeError(
78 `Cannot poll as 'interval' must be a non-negative finite number: current value is ${interval}`,
79 );
80 }
81
82 const delayOptions = signal ? { signal } : undefined;
83
84 while (true) {
85 signal?.throwIfAborted();
86
87 const result = await fn();
88
89 if (isDone(result)) {
90 return result;
91 }
92
93 await delay(interval, delayOptions);
94 }
95}

Callers 1

poll_test.tsFile · 0.90

Calls 2

delayFunction · 0.90
fnFunction · 0.70

Tested by

no test coverage detected