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

By Esteban Trillo2 min readupdated Learning EffectEffectTypeScript

Carry both at once. The types still say what succeeds, fails, and needs.
Carry both at once. The types still say what succeeds, fails, and needs.

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

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

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

This gives one Effect with both results.

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.

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

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.

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

Which part must happen first?

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:

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 onceparseUserId 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.parseUserIdruns firstconcurrency: unboundedfetchUserneeds UserRepofetchInvoicesneeds InvoiceClient{ user, invoices }one EffectuserId

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.

Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.