You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create and run your very first Effect program using Effect.succeed and Effect.runSync.
tags
getting-started
hello-world
beginner
succeed
first-program
rule
description
Create your first Effect program with Effect.succeed.
related
setup-new-project
effects-are-lazy
execute-with-runpromise
author
Paul Philp
Hello World: Your First Effect
Guideline
Create your first Effect using Effect.succeed to wrap a value, then run it
with Effect.runSync to see the result.
Rationale
Every journey starts with "Hello World". In Effect, you create computations
by describing what you want to happen, then you run them. This separation is
what makes Effect powerful.
Good Example
import{Effect}from"effect";// Step 1: Create an Effect that succeeds with a valueconsthelloWorld=Effect.succeed("Hello, Effect!");// Step 2: Run the Effect and get the resultconstresult=Effect.runSync(helloWorld);console.log(result);// "Hello, Effect!"
Building Up
Now let's add a side effect with Effect.log:
import{Effect}from"effect";constprogram=Effect.gen(function*(){yield*Effect.log("Starting my first Effect program...");constmessage="Hello, Effect!";yield*Effect.log(`Message: ${message}`);returnmessage;});Effect.runSync(program);// Output:// timestamp=... level=INFO message="Starting my first Effect program..."// timestamp=... level=INFO message="Message: Hello, Effect!"
Key Concepts
Effect.succeed(value) - Creates an Effect that always succeeds with the given value
Effect.log(message) - Creates an Effect that logs a message
Effect.gen - A way to write sequential Effects like async/await
Effect.runSync - Executes the Effect and returns the result