Skip to content

Latest commit

 

History

History
84 lines (64 loc) · 2.11 KB

File metadata and controls

84 lines (64 loc) · 2.11 KB
title Hello World: Your First Effect
id getting-started-hello-world
skillLevel beginner
applicationPatternId getting-started
lessonOrder 2
summary 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 value
const helloWorld = Effect.succeed("Hello, Effect!");

// Step 2: Run the Effect and get the result
const result = Effect.runSync(helloWorld);

console.log(result); // "Hello, Effect!"

Building Up

Now let's add a side effect with Effect.log:

import { Effect } from "effect";

const program = Effect.gen(function* () {
  yield* Effect.log("Starting my first Effect program...");
  const message = "Hello, Effect!";
  yield* Effect.log(`Message: ${message}`);
  return message;
});

Effect.runSync(program);
// Output:
// timestamp=... level=INFO message="Starting my first Effect program..."
// timestamp=... level=INFO message="Message: Hello, Effect!"

Key Concepts

  1. Effect.succeed(value) - Creates an Effect that always succeeds with the given value
  2. Effect.log(message) - Creates an Effect that logs a message
  3. Effect.gen - A way to write sequential Effects like async/await
  4. Effect.runSync - Executes the Effect and returns the result

What's Next?

  • Learn why Effects are different from Promises
  • Learn how to handle errors with Effect.fail
  • Learn how to transform values with Effect.map