Skip to content

unbounded-poll ​

A loop that awaits a delay (sleep(...), tick(), new Promise((r) => setTimeout(r, ms)), Promise.resolve(), a function of the file that returns such a promise) and ends only when polled state changes, with no deadline, no attempt limit and no AbortSignal: while (!ready) await tick(), for (;;) { if (done) break; await sleep(10); }, or a loop with no way out at all.

Why: the state it waits for may never change: the code that sets it was changed or removed, an event it depends on doesn't come, a test set up the wrong precondition. Then the loop spins forever: the test hangs until the runner's timeout kills the whole file (if it has one), the request never returns, the process never exits. An adopter's test suite hung this way after a refactor stopped the event the loop was waiting for.

✕ Rejectedunbounded-poll
ts
type Scene = { readonly ready: boolean };

const tick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 1));

export async function waitForScene(scene: Scene): Promise<void> {
  while (!scene.ready) await tick();
}

✅ Accepted: a deadline

✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";

type Scene = { readonly ready: boolean };

export type WaitError = Enum<{ TimedOut: { ms: number } }>;
export const WaitError = constructors<WaitError>();

const tick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 1));

export async function waitForScene(scene: Scene, ms: number): Promise<Result<void, WaitError>> {
  const deadline = performance.now() + ms;
  while (!scene.ready) {
    if (performance.now() > deadline) return err(WaitError.TimedOut({ ms }));
    await tick();
  }
  return undefined;
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";

type Scene = { readonly ready: boolean };

export type WaitError = Enum<{ TimedOut: { ms: number } }>;
export const WaitError = constructors<WaitError>();

const tick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 1));

export async function waitForScene(scene: Scene, ms: number): Promise<Result<void, WaitError>> {
  const deadline = performance.now() + ms;
  while (!scene.ready) {
    if (performance.now() > deadline) return err(({ tag: "TimedOut", ms } satisfies WaitError as WaitError));
    await tick();
  }
  return undefined;
}

An attempt limit (for (let i = 0; i < 50 && !scene.ready; i++) await tick()) or an AbortSignal the caller can abort (signal.throwIfAborted() in the loop, or the signal passed to the delay) count too. Better still, wait for the event that changes the state, so there is nothing to poll.

What counts ​

  • A poll: a while, do ... while or for (;;) loop whose own body (not a nested function or loop) awaits a delay: a call named sleep, delay, wait, waitForTimeout, tick, nextTick, pause, nextFrame, setTimeout, setImmediate, yield, requestAnimationFrame or advanceTimersByTimeAsync; Promise.resolve(); a new Promise whose executor starts a timer; or a function of the file that does.
  • A way out: a test that ends the loop (its condition, or an if that breaks out, returns, throws or calls fail(...), panic(...), unreachable(...), process.exit(...)) and reads the clock (Date.now(), performance.now(), new Date(), process.hrtime, or a binding made from one inside the loop: const left = end - Date.now()), a counter the loop changes (i++, left -= 1), or a name that says it is a limit (deadline, timeout, until, attempts, retries, maxTries, remaining, ...); or a signal, aborted or throwIfAborted anywhere in the loop.
  • Not a poll: a loop whose awaits all produce what it tests (const item = await next(); if (item === undefined) break;): it waits for data, not for time to pass. Nor a loop that moves the state it tests forward itself: the test reads something the loop changes, passes to a call, or calls a changing method of (queue.shift(), timers.delete(id); while (world.tick < end) { advance(world); await yieldToLoop(); }). for ... of and for await ... of loops aren't checked.

A timer or interval kept in a binding and not cleared on every way out of its function is unreleased-resource's; this rule is about a loop with no way out.

What the rule can't see: a delay under a name it doesn't know, or one imported from another file under a name not in the list; a limit kept in a binding whose name says nothing and that the loop doesn't count (if (Date.now() > t) break counts, if (late(t)) break doesn't); a timeout that an awaited call enforces itself. A limit name in the test is taken at its word, and so is passing the tested value to any call in the loop (log(scene) counts as moving it forward).

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra