Hyperlinkv0.8.0-beta.28

Schedule

Schedule.spacedconsteffect/Schedule.ts:1823
(duration: Duration.Input): Schedule<number>

Returns a schedule that recurs continuously, each repetition spaced the specified duration from the last run.

When to use

Use when each delay should start after the previous action completes.

Example (Repeating with fixed spacing)

import { Console, Effect, Schedule } from "effect"

// Basic spaced schedule - runs every 2 seconds
const everyTwoSeconds = Schedule.spaced("2 seconds")

// Heartbeat that runs indefinitely with fixed spacing
const heartbeat = Effect.gen(function*() {
  yield* Console.log("Heartbeat")
}).pipe(
  Effect.repeat(everyTwoSeconds)
)

// Limited repeat - run only 5 times with 1-second spacing
const limitedTask = Effect.gen(function*() {
  yield* Console.log("Executing scheduled task...")
  yield* Effect.sleep("500 millis") // simulate work
  return "Task completed"
}).pipe(
  Effect.repeat(
    Schedule.spaced("1 second").pipe(Schedule.upTo({ times: 5 }))
  )
)

// Simple spaced schedule with limited repetitions
const limitedSpaced = Schedule.max([
  Schedule.spaced("100 millis"),
  Schedule.recurs(5) // at most 5 times
])

const program = Effect.gen(function*() {
  yield* Console.log("Starting spaced execution...")

  yield* Effect.repeat(
    Effect.succeed("work item"),
    limitedSpaced
  )

  yield* Console.log("Completed executions")
})
constructorsfixed
export const spaced = (duration: Duration.Input): Schedule<number> => {
  const decoded = Duration.fromInputUnsafe(duration)
  return fromStepWithMetadata(effect.succeed((meta) => effect.succeed([meta.attempt - 1, decoded])))
}
Referenced by 2 symbols