# map vs flatMap in Effect (Learning Effect, Part 4)

- Published: 2026-07-13
- Updated: 2026-10-21
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-4-map-vs-flatmap/

I kept mixing up `map` and `flatMap`.

The rule is simple.

Use `map` when the callback returns a plain value.

Use `flatMap` when the callback returns an Effect.

That is it.

## The same names in plain TypeScript

Arrays have both operators too.

```ts
const names = ["ada lovelace", "grace hopper"];

names.map((name) => name.split(" "));
// [["ada", "lovelace"], ["grace", "hopper"]]

names.flatMap((name) => name.split(" "));
// ["ada", "lovelace", "grace", "hopper"]
```

The callback returns an array.

`map` keeps the nesting.

`flatMap` flattens it.

Effect works the same way. Swap the array for an Effect.

`Promise.then` hides this. It flattens whatever you return, so you never had to pick.

Effect makes you pick.

That felt like extra work at first. Now it tells me what kind of step I am writing.

## `map` changes the value

```ts
const program = Effect.succeed("ada").pipe(
  Effect.map((name) => name.toUpperCase()),
);
```

`toUpperCase()` returns a plain string.

So this is `map`.

**Quiz:** Should this use map or flatMap? name => name.toUpperCase()

- map
- flatMap
- runPromise

**Answer:** map — The callback returns a plain string, not an Effect. That is map.

## `flatMap` continues the program

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

`fetchUser(id)` returns an Effect.

So this is `flatMap`.

**Quiz:** Should this use map or flatMap? id => fetchUser(id)

- map
- flatMap
- succeed

**Answer:** flatMap — fetchUser returns another Effect, so flatMap connects it to the current Effect.

## The smell

If you use `map` with a function that returns an Effect, you get an Effect inside an Effect.

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

That type is trying to tell you something.

You probably wanted `flatMap`.

**Quiz:** What usually caused Effect inside Effect?

- Using map when the callback returned an Effect
- Using too many classes
- Running the program too early

**Answer:** Using map when the callback returned an Effect — map wraps the callback result as the success value. If that result is already an Effect, you get nesting.

## The other direction

The reverse mistake is louder.

```ts
const program = Effect.succeed("ada").pipe(
  Effect.flatMap((name) => name.toUpperCase()),
);
```

`flatMap` expects the callback to return an Effect.

This one returns a string.

`toUpperCase()` gives back a plain value, so `map` was the right tool.

TypeScript stops you here. The error says `string` is not assignable to `Effect`.

The nested case is quieter. It compiles.

How to spot it: hover the program in your editor. Two `Effect` types in a row means nesting.

Both mistakes have the same fix. Look at what the callback returns.

Same question as before.

## What I am taking with me

The question is not “which operator do I like?”

The question is:

> What does my callback return?

Plain value: `map`.

Effect value: `flatMap`.
