# One Small Real Program in Effect (Learning Effect, Part 11)

- Published: 2026-07-20
- Updated: 2026-10-22
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-11-one-small-real-program/

Now I want one small program.

Not a production app.

Not an architecture diagram.

Just enough code to see the pieces touch.

## The flow

The program will:

**One request through the program.** Inside the GET handler, which is the edge, the program reads a user id from the request, parses it, and fetches the user. A found user becomes a 200 JSON response. If parsing fails, catchTag turns InvalidUserId into a 400. If the fetch fails, catchTag turns UserNotFound into a 404. The whole thing runs once, at the edge.

That is a real shape.

## The plain version

```ts
export async function GET(request: Request) {
  const input = new URL(request.url).searchParams.get("id") ?? "";

  try {
    const id = parseUserId(input);
    const user = await fetchUser(id);
    return Response.json(user);
  } catch (error) {
    if (error instanceof UserNotFound) {
      return new Response("User not found", { status: 404 });
    }
    if (error instanceof InvalidUserId) {
      return new Response("Invalid user id", { status: 400 });
    }
    throw error;
  }
}
```

It works.

It is also the version most people write first.

Nothing checks that list.

If `fetchUser` learns a new way to fail, this still compiles. The new error falls through to `throw error`.

## The core program

```ts
const getUserResponse = (input: string) =>
  parseUserId(input).pipe(
    Effect.flatMap(fetchUser),
    Effect.map((user) => Response.json(user)),
    Effect.catchTag("UserNotFound", () =>
      Effect.succeed(new Response("User not found", { status: 404 })),
    ),
  );
```

Read it straight down.

Parse.

Fetch.

Turn success into JSON.

Handle one known error.

**Quiz:** Which error is handled here?

- InvalidUserId
- UserNotFound
- Every possible error

**Answer:** UserNotFound — catchTag only handles UserNotFound. Other errors stay in the error slot.

## The type tells the truth

After handling `UserNotFound`, the type might still say:

```ts
Effect.Effect<Response, InvalidUserId, UserRepo>;
```

That means:

- success: `Response`
- error: `InvalidUserId`
- requirement: `UserRepo`

There is still work to do before running it.

We need to decide what bad input means.

We need to provide `UserRepo`.

**Quiz:** What still must be provided before this program can run?

- Response
- UserRepo
- UserNotFound

**Answer:** UserRepo — The third slot is still UserRepo, so the edge must provide that dependency.

## Run it at the edge

```ts
export async function GET(request: Request) {
  const id = new URL(request.url).searchParams.get("id") ?? "";

  const program = getUserResponse(id).pipe(
    Effect.provide(UserRepoLive),
    Effect.catchTag("InvalidUserId", () =>
      Effect.succeed(new Response("Invalid user id", { status: 400 })),
    ),
  );

  return Effect.runPromise(program);
}
```

The runner appears once.

At the edge.

That feels right.

## The mistake: running with errors left over

`Effect.runPromise` accepts a program with errors still in the slot.

If one of them happens, the promise rejects.

At the edge, that usually means a 500.

Before you run, hover `program`. Here it should read:

```ts
Effect.Effect<Response, never, never>;
```

Nothing left to handle. Nothing left to provide.

If the error slot is not `never`, decide what each error means first.

A 400, a 404, a retry, or a log line.

Each one should be a choice you can see.

## What I learned from the first pass

Effect is not one idea.

It is a stack of small ideas:

- describe work before running it
- make errors visible
- make dependencies visible
- compose the happy path without losing the failure path
- run the program at the edge

I still have a lot to learn.

But now I can read the first line of the map.

```ts
Effect.Effect<Success, Error, Requirements>;
```

That is enough to keep going.

> **Note:** Next I would learn schedules, retries, scopes, and streams. But not yet. The
> basics should feel boring first.
