# flatMap in Effect: Do This, Then That (Learning Effect, Part 3)

- Published: 2026-07-12
- Updated: 2026-10-22
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-3-flatmap-do-this-then-that/

A useful program rarely does one thing.

It parses an id. Then it fetches a user. Then it checks a permission. Then it writes something.

That is where `flatMap` shows up.

`flatMap` means:

> If this Effect succeeds, use its value to choose the next Effect.

## The plain version

Here is that chain with promises:

```ts
const getUser = async (input: string): Promise<User> => {
  const id = parseUserId(input);
  const user = await fetchUser(id);

  return user;
};
```

It reads well.

Each line waits for the one above it.

The type says `Promise<User>`.

It does not say the id can be invalid. It does not say the user can be missing.

Both failures leave through `throw`. The caller finds out at runtime.

The caller also cannot tell which step failed without reading both functions.

With Effect, the chain looks almost the same. The difference is in the type.

## A small chain

```ts
const parseUserId = (input: string) =>
  Number.isInteger(Number(input))
    ? Effect.succeed(Number(input))
    : Effect.fail(new InvalidUserId(input));

const fetchUser = (id: number) =>
  id === 1
    ? Effect.succeed({ id, name: "Ada" })
    : Effect.fail(new UserNotFound(id));

const program = parseUserId("1").pipe(Effect.flatMap((id) => fetchUser(id)));
```

The second step needs the value from the first step.

That is why this is `flatMap`.

**Quiz:** Why does this use flatMap?

- Because fetchUser returns another Effect
- Because parseUserId is async
- Because flatMap runs the program immediately

**Answer:** Because fetchUser returns another Effect — flatMap is for callbacks that return an Effect. It connects the next Effect to the previous success value.

## Failure skips the rest

If `parseUserId("abc")` fails, `fetchUser` does not run.

**A flatMap chain with an early exit.** parseUserId turns the input into an id. flatMap hands that id to fetchUser, which turns it into a User. If parseUserId fails, the chain stops there with InvalidUserId and fetchUser never runs. If fetchUser fails, the chain ends with UserNotFound. Both failures land in the error slot.

That is the part I like.

The happy path reads straight down. The failure path stays in the type.

```ts
Effect.Effect<User, InvalidUserId | UserNotFound, never>;
```

The program can fail in either step.

**Quiz:** If parseUserId fails, what happens to fetchUser?

- It still runs with id = 0
- It does not run
- It runs twice

**Answer:** It does not run — flatMap only calls the next function when the previous Effect succeeds.

## The mistake: a callback that returns nothing

Braces change what an arrow function returns.

A one-line arrow returns its expression. A block body returns nothing unless you write `return`.

Effect code is full of arrows, so this comes up often.

```ts
parseUserId("1").pipe(
  Effect.flatMap((id) => {
    fetchUser(id);
  }),
);
```

This builds a `fetchUser` Effect and drops it.

The callback returns `void`.

TypeScript catches this one. The error says `void` is not assignable to `Effect`.

When you see that message, look for a missing `return`.

The fix is one word.

Or drop the braces:

```ts
parseUserId("1").pipe(Effect.flatMap((id) => fetchUser(id)));
```

## The part that finally clicked

This:

```ts
parseUserId("1").pipe(Effect.flatMap(fetchUser));
```

is the Effect version of:

```ts
const id = parseUserId("1");
const user = fetchUser(id);
```

Except the failures are not hiding.

They are part of the result.

> **Note:** My current rule: if the next step returns an Effect, reach for flatMap.
