Hyperlinkv0.8.0-beta.28

Effect

Effect.syncconsteffect/Effect.ts:1141
<A>(thunk: LazyArg<A>): Effect<A>

Creates an Effect that represents a synchronous side-effectful computation.

When to use

Use when you need to wrap a synchronous side-effectful operation that is not expected to throw.

Details

The provided function is evaluated lazily when the effect runs.

Gotchas

The function must not throw. If it throws, the thrown value is treated as a defect, not as a typed failure. Use try when throwing is expected.

Example (Capturing synchronous logging in an Effect)

import { Effect } from "effect"

const log = (message: string) =>
  Effect.sync(() => {
    console.log(message) // side effect
  })

//      ┌─── Effect<void, never, never>
//      ▼
const program = log("Hello, World!")
constructorstry_
Source effect/Effect.ts:11411 lines
export const sync: <A>(thunk: LazyArg<A>) => Effect<A> = internal.sync
Referenced by 103 symbols