Schedule<number, unknown, never, never>Returns a new Schedule that will recur forever.
Details
The output of the schedule is the current count of its repetitions thus far
(i.e. 0, 1, 2, ...).
Example (Repeating forever)
import { Console, Effect, Schedule } from "effect"
// A schedule that runs forever with no delay
const infiniteSchedule = Schedule.forever
const program = Effect.gen(function*() {
yield* Effect.repeat(
Effect.gen(function*() {
yield* Console.log("Running forever...")
return "continuous-task"
}),
infiniteSchedule.pipe(Schedule.upTo({ times: 5 })) // Limit for demo
)
})export const const forever: Schedule<number>const forever: {
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; <…;
}
Returns a new Schedule that will recur forever.
Details
The output of the schedule is the current count of its repetitions thus far
(i.e. 0, 1, 2, ...).
Example (Repeating forever)
import { Console, Effect, Schedule } from "effect"
// A schedule that runs forever with no delay
const infiniteSchedule = Schedule.forever
const program = Effect.gen(function*() {
yield* Effect.repeat(
Effect.gen(function*() {
yield* Console.log("Running forever...")
return "continuous-task"
}),
infiniteSchedule.pipe(Schedule.upTo({ times: 5 })) // Limit for demo
)
})
forever: 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 spaced: (
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")
})
spaced(import DurationDuration.zero)