Open this lesson on a wider screen to play it. The narrated transcript and animated code need room to sit side by side.
← All lessonsimport { Data, Effect } from "effect";
class MissingId extends Data.TaggedError("MissingId")<{}> {}
// yield* Effect.fail short-circuits the block the way throw leaves a function —
// but the failure is typed, so it lands in E instead of vanishing.
const lookup = (id: string) =>
Effect.gen(function* () {
if (id === "") return yield* Effect.fail(new MissingId());
const name = yield* Effect.succeed("Ada");
return name;
});
// ^? (id: string) => Effect.Effect<string, MissingId, never>
// States are executed, so inspect the failure with runSyncExit, never a throw.
console.log(Effect.runSyncExit(lookup(""))); // Exit Failure: MissingId