Skip to content

pollUntil

pollUntil<A>(options): <E>(task) => TaskResult<E, A>

Defined in: Core/TaskResult.ts:210

Polls a TaskResult repeatedly until the success value satisfies a predicate. Stops immediately and returns Err if the task fails. An AbortSignal passed at the call site is forwarded to each attempt; the loop also checks the signal before starting a new poll so cancellation stops promptly.

delay accepts a fixed number of milliseconds or a function (attempt) => ms for a computed delay — useful for starting fast and slowing down over time.

A

number | (attempt) => number

(a) => boolean

<E>(task): TaskResult<E, A>

E

TaskResult<E, A>

TaskResult<E, A>

const checkJob = (id: string): TaskResult<string, { status: "pending" | "done" }> =>
  TaskResult.tryCatch(
    (signal) => fetch(`/jobs/${id}`, { signal }).then(r => r.json()),
    String
  );

// Fixed delay: poll every 2s
pipe(
  checkJob(jobId),
  TaskResult.pollUntil({ when: job => job.status === "done", delay: 2000 }),
);

// Computed delay: 1s, 2s, 3s, ...
pipe(
  checkJob(jobId),
  TaskResult.pollUntil({ when: job => job.status === "done", delay: n => n * 1000 }),
);