effect-io-ai

Package: effect
Module: Effect

Effect.whileLoop

Executes a body effect repeatedly while a condition holds true.

Example (Repeating an effectful loop)

import { Effect } from "effect"

let counter = 0

const program = Effect.whileLoop({
  while: () => counter < 5,
  body: () => Effect.sync(() => ++counter),
  step: (n) => console.log(`Current count: ${n}`)
})

Effect.runPromise(program)
// Output:
// Current count: 1
// Current count: 2
// Current count: 3
// Current count: 4
// Current count: 5

Signature

declare const whileLoop: <A, E, R>(options: { readonly while: LazyArg<boolean>; readonly body: LazyArg<Effect<A, E, R>>; readonly step: (a: A) => void; }) => Effect<void, E, R>

Source

Since v2.0.0