# Run Effects at the Edge (Learning Effect, Part 9)

- Published: 2026-07-18
- Updated: 2026-10-20
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-9-run-effects-at-the-edge/

An Effect does not run when you create it.

That was the first weird part.

The next question is where to run it.

My current answer:

> Run Effects at the edge.

## The plain version

Promises start when you call them.

```ts
const getUser = async (id: number) => {
  return db.users.findById(id);
};

const pending = getUser(1); // the query is already running
```

Every async function is its own little edge.

There is no single place where work begins.

That makes it hard to say when something runs.

A promise also cannot be restarted.

To retry, you need the function again, not the promise you already have.

Effect splits that in two.

Building the work is one step. Starting it is another.

## The edge is where the outside world calls you

An HTTP handler is an edge.

A CLI command is an edge.

A cron job is an edge.

That is where you turn the Effect description into real work.

```ts
export async function GET(request: Request) {
  const program = handleRequest(request);
  const response = await Effect.runPromise(program);

  return response;
}
```

**Quiz:** Which line starts the work?

- const program = handleRequest(request)
- Effect.runPromise(program)
- return response

**Answer:** Effect.runPromise(program) — Creating the Effect builds a description. runPromise starts it.

## Helpers should usually return Effects

This is the trap:

```ts
const getUser = async (id: number) => Effect.runPromise(fetchUser(id));
```

That throws away a lot.

Now the caller gets a Promise. The typed error is gone. The requirement handling is hidden. Composition gets worse.

Better:

```ts
const getUser = (id: number) => fetchUser(id);
```

Let the edge run it.

**Quiz:** Should a low-level helper usually call runPromise?

- Yes, always
- No, usually return the Effect
- Only if the success type is string

**Answer:** No, usually return the Effect — Returning the Effect keeps errors and requirements visible for the caller.

## Why this feels familiar

This is like keeping business logic out of your route handler.

The route handler receives the world.

The program describes the work.

The runner starts it.

That split keeps the center of the app easier to test and easier to read.

## The mistake: runSync on async work

Effect has more than one runner.

`Effect.runSync` returns the value directly. No `await`.

It only works when every step is synchronous.

`parseUserId` from the first part has no async steps, so `runSync` works there.

```ts
const program = Effect.sleep("1 second").pipe(Effect.map(() => "done"));

Effect.runSync(program);
```

This compiles.

It throws at runtime with an `AsyncFiberError`.

The type does not track sync versus async. You find out when it runs.

How to spot it: `runSync` on anything that fetches, sleeps, or talks to a database.

At an edge that can wait, use `runPromise`.

The fix is small. Swap the runner, add `await`, and let the edge wait.
