# Concurrency Without Guessing in Effect (Learning Effect, Part 10)

- Published: 2026-07-19
- Updated: 2026-10-21
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-10-concurrency-without-guessing/

At some point, a program needs two things that do not depend on each other.

Fetch the user.

Fetch the invoices.

Those can run at the same time.

## The plain version

```ts
const [user, invoices] = await Promise.all([
  fetchUser(userId),
  fetchInvoices(userId),
]);
```

This runs both at once.

There is also no type for what failed.

Both fetches can fail in different ways. The rejection carries one of them.

If `fetchUser` rejects, `Promise.all` rejects too.

`fetchInvoices` keeps going in the background. Nothing cancels it.

The caller gets an `unknown` error in its `catch`.

If you want the user even when invoices fail, you switch to `Promise.allSettled` and unpack the results by hand.

## Run both

```ts
const program = Effect.all({
  user: fetchUser(userId),
  invoices: fetchInvoices(userId),
});
```

This gives one Effect with both results.

```ts
Effect.Effect<
  { user: User; invoices: Invoice[] },
  UserNotFound | InvoiceApiDown,
  UserRepo | InvoiceClient
>;
```

That type is doing useful work.

It shows the combined success, combined errors, and combined requirements.

**Quiz:** What is the success value of Effect.all with user and invoices?

- Only User
- An object with user and invoices
- Only InvoiceApiDown

**Answer:** An object with user and invoices — Effect.all combines the successful values into the same shape you passed in.

## Parallel when it is safe

Some work must be sequential.

You cannot fetch invoices for a user id before you have parsed the user id.

But once you have the id, independent calls can run together.

```ts
const program = parseUserId(input).pipe(
  Effect.flatMap((userId) =>
    Effect.all({
      user: fetchUser(userId),
      invoices: fetchInvoices(userId),
    }),
  ),
);
```

**Quiz:** Which part must happen first?

- parseUserId
- fetchUser and fetchInvoices
- Formatting the final object

**Answer:** parseUserId — The id is needed before either fetch can run. After that, the two fetches are independent.

## The mistake: assuming all means parallel

`Effect.all` runs one Effect at a time by default.

The examples above fetch the user first, then the invoices.

To run them together, say so:

```ts
const program = Effect.all(
  {
    user: fetchUser(userId),
    invoices: fetchInvoices(userId),
  },
  { concurrency: "unbounded" },
);
```

Now both start together.

If one fails, Effect interrupts the other.

Nothing keeps running in the background.

**Parse first, then fetch both at once.** parseUserId runs first and produces a userId. With concurrency set to unbounded, fetchUser (which needs UserRepo) and fetchInvoices (which needs InvoiceClient) start together with that id. Their results join into one Effect holding { user, invoices }. If one fetch fails, Effect interrupts the other.

`concurrency` also takes a number.

`{ concurrency: 2 }` runs at most two at a time. That helps when the list is long and the API has a rate limit.

How to spot it: an `Effect.all` with no `concurrency` option, in code you expected to run in parallel.

Without the option, the code is still correct.

It is only slower than you expected.

## The nice part

In normal async code, I often have to inspect the `Promise.all` shape and then think about errors separately.

Effect keeps the shape together.

Success. Error. Requirements.

The same three slots again.
