API reference@evolu/commonTask › repeat

function repeat<T, E, D, Output>(
  task: Task<T, E, D>,
  schedule: Schedule<Output, T>,
  __namedParameters?: RepeatOptions<T, Output>,
): Task<T, E, D>;

Defined in: packages/common/src/Task.ts:2133

Repeats a Task according to a Schedule.

Runs the Task, then checks the schedule to determine if it should repeat. The schedule controls how many repetitions occur and the delay between them. Continues until the schedule returns Err(Done<void>) or the Task fails.

With take(n), the task runs n+1 times (initial run plus n repetitions).

Also works with NextTask — when the Task returns Err(Done<D>), repeat stops and propagates the done signal.

Example

import { fixed, take } from "@evolu/common/schedule";
import { repeat } from "@evolu/common";

// Heartbeat every 30 seconds (runs forever until aborted)
const heartbeat = repeat(sendHeartbeat, fixed("30s"));

// Poll 4 times total (initial + 3 repetitions), 1 second apart
const poll = repeat(checkStatus, take(3)(fixed("1s")));

// Process queue items until empty (NextTask pattern)
const processQueue: NextTask<Item, ProcessError, void> = async (run) => {
  const item = queue.dequeue();
  if (!item) return err(done()); // Queue empty, stop
  await process(item);
  return ok(item);
};

const result = await run(repeat(processQueue, fixed("100ms")));
if (!result.ok && result.error.type === "Done") {
  console.log("Queue exhausted");
}