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
- 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
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