Effect by Example: Hello World

Tags:

The most basic effect program

import { import Effect
@since2.0.0@categorymodels@since2.0.0@categorymodels@since2.0.0@categorymodels
Effect
} from "effect";
const const main: Effect.Effect<void, never, never>main = import Effect
@since2.0.0@categorymodels@since2.0.0@categorymodels@since2.0.0@categorymodels
Effect
.const sync: <void>(thunk: LazyArg<void>) => Effect.Effect<void, never, never>
Creates an `Effect` that represents a synchronous side-effectful computation. **Details** The provided function (`thunk`) must not throw errors; if it does, the error will be treated as a "defect". This defect is not a standard error but indicates a flaw in the logic that was expected to be error-free. You can think of it similar to an unexpected crash in the program, which can be further managed or logged using tools like {@link catchAllDefect } . **When to Use** Use this function when you are sure the operation will not fail.
@see{@link try_try} for a version that can handle failures.@example```ts // Title: Logging a Message import { Effect } from "effect" const log = (message: string) => Effect.sync(() => { console.log(message) // side effect }) // ┌─── Effect<void, never, never> // ▼ const program = log("Hello, World!") ```@since2.0.0@categoryCreating Effects
sync
(() => var console: Consoleconsole.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
("Hello World"));
import Effect
@since2.0.0@categorymodels@since2.0.0@categorymodels@since2.0.0@categorymodels
Effect
.const runSync: <void, never>(effect: Effect.Effect<void, never, never>) => void
Executes an effect synchronously, running it immediately and returning the result. **Details** This function evaluates the provided effect synchronously, returning its result directly. It is ideal for effects that do not fail or include asynchronous operations. If the effect does fail or involves async tasks, it will throw an error. Execution stops at the point of failure or asynchronous operation, making it unsuitable for effects that require asynchronous handling. **Important**: Attempting to run effects that involve asynchronous operations or failures will result in exceptions being thrown, so use this function with care for purely synchronous and error-free effects. **When to Use** Use this function when: - You are sure that the effect will not fail or involve asynchronous operations. - You need a direct, synchronous result from the effect. - You are working within a context where asynchronous effects are not allowed. Avoid using this function for effects that can fail or require asynchronous handling. For such cases, consider using {@link runPromise } or {@link runSyncExit } .
@see{@link runSyncExit} for a version that returns an `Exit` type instead of throwing an error.@example```ts // Title: Synchronous Logging import { Effect } from "effect" const program = Effect.sync(() => { console.log("Hello, World!") return 1 }) const result = Effect.runSync(program) // Output: Hello, World! console.log(result) // Output: 1 ```@example// Title: Incorrect Usage with Failing or Async Effects import { Effect } from "effect" try { // Attempt to run an effect that fails Effect.runSync(Effect.fail("my error")) } catch (e) { console.error(e) } // Output: // (FiberFailure) Error: my error try { // Attempt to run an effect that involves async work Effect.runSync(Effect.promise(() => Promise.resolve(1))) } catch (e) { console.error(e) } // Output: // (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work@since2.0.0@categoryRunning Effects
runSync
(const main: Effect.Effect<void, never, never>main);