(interval: Duration.Input): Schedule<number>Returns a Schedule that recurs on the specified fixed interval and
outputs the number of repetitions of the schedule so far.
When to use
Use when recurrences should stay aligned to a regular cadence.
Gotchas
If the action run between recurrences takes longer than the interval, the next recurrence happens immediately, but missed intervals are not replayed.
|-----interval-----|-----interval-----|-----interval-----|
|---------action--------||action|-----|action|-----------|Example (Repeating on fixed intervals)
import { Console, Effect, Schedule } from "effect"
// Fixed interval schedule - recurs on a one-second cadence
const everySecond = Schedule.fixed("1 second")
// Health check that runs at fixed intervals
const healthCheck = Effect.gen(function*() {
yield* Console.log("Health check")
yield* Effect.sleep("200 millis") // simulate health check work
return "healthy"
}).pipe(
Effect.repeat(Schedule.fixed("2 seconds").pipe(Schedule.upTo({ times: 5 })))
)
// Difference between fixed and spaced:
// - fixed: maintains constant rate regardless of action duration
// - spaced: waits for the duration AFTER each action completes
const longRunningTask = Effect.gen(function*() {
yield* Console.log("Task started")
yield* Effect.sleep("1.5 seconds") // Longer than interval
yield* Console.log("Task completed")
return "done"
})
// Fixed schedule: if task takes 1.5s but interval is 1s,
// next execution happens immediately (no pile-up)
const fixedSchedule = longRunningTask.pipe(
Effect.repeat(Schedule.fixed("1 second").pipe(Schedule.upTo({ times: 3 })))
)
// Comparing with spaced (waits 1s AFTER each task)
const spacedSchedule = longRunningTask.pipe(
Effect.repeat(Schedule.spaced("1 second").pipe(Schedule.upTo({ times: 3 })))
)
const program = Effect.gen(function*() {
yield* Console.log("=== Fixed Schedule Demo ===")
yield* fixedSchedule
yield* Console.log("=== Spaced Schedule Demo ===")
yield* spacedSchedule
})export const const fixed: (
interval: Duration.Input
) => Schedule<number>
Returns a Schedule that recurs on the specified fixed interval and
outputs the number of repetitions of the schedule so far.
When to use
Use when recurrences should stay aligned to a regular cadence.
Gotchas
If the action run between recurrences takes longer than the interval, the
next recurrence happens immediately, but missed intervals are not replayed.
|-----interval-----|-----interval-----|-----interval-----|
|---------action--------||action|-----|action|-----------|
Example (Repeating on fixed intervals)
import { Console, Effect, Schedule } from "effect"
// Fixed interval schedule - recurs on a one-second cadence
const everySecond = Schedule.fixed("1 second")
// Health check that runs at fixed intervals
const healthCheck = Effect.gen(function*() {
yield* Console.log("Health check")
yield* Effect.sleep("200 millis") // simulate health check work
return "healthy"
}).pipe(
Effect.repeat(Schedule.fixed("2 seconds").pipe(Schedule.upTo({ times: 5 })))
)
// Difference between fixed and spaced:
// - fixed: maintains constant rate regardless of action duration
// - spaced: waits for the duration AFTER each action completes
const longRunningTask = Effect.gen(function*() {
yield* Console.log("Task started")
yield* Effect.sleep("1.5 seconds") // Longer than interval
yield* Console.log("Task completed")
return "done"
})
// Fixed schedule: if task takes 1.5s but interval is 1s,
// next execution happens immediately (no pile-up)
const fixedSchedule = longRunningTask.pipe(
Effect.repeat(Schedule.fixed("1 second").pipe(Schedule.upTo({ times: 3 })))
)
// Comparing with spaced (waits 1s AFTER each task)
const spacedSchedule = longRunningTask.pipe(
Effect.repeat(Schedule.spaced("1 second").pipe(Schedule.upTo({ times: 3 })))
)
const program = Effect.gen(function*() {
yield* Console.log("=== Fixed Schedule Demo ===")
yield* fixedSchedule
yield* Console.log("=== Spaced Schedule Demo ===")
yield* spacedSchedule
})
fixed = (interval: Duration.Inputinterval: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input): interface Schedule<out Output, in Input = unknown, out Error = never, out Env = never>A Schedule defines a strategy for repeating or retrying effects based on some policy.
Example (Defining retry and repeat schedules)
import { Console, Data, Effect, Schedule } from "effect"
class NetworkError extends Data.TaggedError("NetworkError")<{
readonly attempt: number
}> {}
// Basic retry schedule - retry up to 3 times with exponential backoff
const retrySchedule = Schedule.max([
Schedule.exponential("100 millis"),
Schedule.recurs(3)
])
// Basic repeat schedule - repeat every 30 seconds forever
const repeatSchedule: Schedule.Schedule<number, unknown, never> = Schedule
.spaced("30 seconds")
const program = Effect.gen(function*() {
let attempts = 0
const result1 = yield* Effect.retry(
Effect.gen(function*() {
attempts++
if (attempts < 3) {
return yield* Effect.fail(new NetworkError({ attempt: attempts }))
}
return "Success"
}),
retrySchedule
)
console.log(result1) // "Success"
yield* Console.log("heartbeat").pipe(
Effect.repeat(repeatSchedule.pipe(Schedule.upTo({ times: 5 })))
)
})
The Schedule namespace contains types and utilities for working with schedules.
Schedule<number> => {
const const window: numberwindow = import DurationDuration.const toMillis: (self: Input) => numberConverts a Duration to milliseconds.
Example (Converting durations to milliseconds)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.seconds(5))) // 5000
console.log(Duration.toMillis(Duration.minutes(2))) // 120000
toMillis(import DurationDuration.const fromInputUnsafe: (
input: Input
) => Duration
Decodes a Duration.Input into a Duration.
When to use
Use when the input has already been validated or comes from a trusted source
and throwing is acceptable for invalid duration syntax.
Gotchas
If the input is not a valid Duration.Input, it throws an error.
Example (Decoding duration inputs)
import { Duration } from "effect"
const duration1 = Duration.fromInputUnsafe(1000) // 1000 milliseconds
const duration2 = Duration.fromInputUnsafe("5 seconds")
const duration3 = Duration.fromInputUnsafe("Infinity")
const duration4 = Duration.fromInputUnsafe([2, 500_000_000]) // 2 seconds and 500ms
fromInputUnsafe(interval: Duration.Inputinterval))
return const fromStepWithMetadata: <
Input,
Output,
EnvX,
ErrorX,
Error,
Env
>(
step: Effect<
(
options: InputMetadata<Input>
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>,
Error,
Env
>
) => Schedule<
Output,
Input,
Error | Pull.ExcludeDone<ErrorX>,
Env | EnvX
>
Creates a Schedule from a step function that receives metadata about the schedule's execution.
Example (Creating a metadata-aware schedule)
import { Cause, Duration, Effect, Schedule } from "effect"
const firstThreeInputs = Schedule.fromStepWithMetadata(Effect.succeed((metadata: Schedule.InputMetadata<string>) => {
if (metadata.attempt > 3) {
return Cause.done("finished")
}
return Effect.succeed([
`attempt ${metadata.attempt}: ${metadata.input}`,
Duration.millis(250)
] as [string, Duration.Duration])
}))
fromStepWithMetadata(import effecteffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(() => {
let let start: numberstart = 0
let let lastRun: numberlastRun = 0
return (meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta) =>
import effecteffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(() => {
if (const window: numberwindow === 0) {
return [meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.attempt: numberattempt - 1, import DurationDuration.const zero: Durationconst zero: {
value: DurationValue;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
A Duration representing zero time.
Example (Referencing the zero duration)
import { Duration } from "effect"
console.log(Duration.toMillis(Duration.zero)) // 0
zero] as type const = [number, Duration.Duration]const
}
if (meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.attempt: numberattempt === 1) {
let start: numberstart = meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.now: numbernow
let lastRun: numberlastRun = meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.now: numbernow + const window: numberwindow
return [0, import DurationDuration.const millis: (millis: number) => DurationCreates a Duration from milliseconds.
Example (Creating durations from milliseconds)
import { Duration } from "effect"
const duration = Duration.millis(1000)
console.log(Duration.toMillis(duration)) // 1000
millis(const window: numberwindow)] as type const = [0, Duration.Duration]const
}
const const runningBehind: booleanrunningBehind = meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.now: numbernow > (let lastRun: numberlastRun + const window: numberwindow)
const const boundary: numberboundary = const window: numberwindow - ((meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.now: numbernow - let start: numberstart) % const window: numberwindow)
const const delay: numberdelay = const runningBehind: booleanrunningBehind ? 0 : const boundary: numberboundary === 0 ? const window: numberwindow : const boundary: numberboundary
let lastRun: numberlastRun = const runningBehind: booleanrunningBehind ? meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.now: numbernow : meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.now: numbernow + const delay: numberdelay
return [meta: InputMetadata<unknown>(parameter) meta: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.InputMetadata<Input>.attempt: numberattempt - 1, import DurationDuration.const millis: (millis: number) => DurationCreates a Duration from milliseconds.
Example (Creating durations from milliseconds)
import { Duration } from "effect"
const duration = Duration.millis(1000)
console.log(Duration.toMillis(duration)) // 1000
millis(const delay: numberdelay)] as type const = [number, Duration.Duration]const
})
}))
}