Why using a Runtime

Imagine we need to execute different programs for multiple endpoints.

With what we have at the moment we need to use Effect.provide every time we want to run an effect:

export async function GET() {
  const main = programGet.pipe(Effect.provide(MainLayer));
  return Effect.runPromise(main);
}

export async function POST() {
  const main = programPost.pipe(Effect.provide(MainLayer));
  return Effect.runPromise(main);
}

export async function DELETE() {
  const main = programDelete.pipe(Effect.provide(MainLayer));
  return Effect.runPromise(main);
}

That's not ideal. We would like to have a centralized system that includes all the services and can run any effect.

Good news! There is a solution for this, it's called Runtime.

What's a Runtime exactly?

As we mentioned at the beginning of the course, an Effect is a description of a program.

By itself Effect doesn't do anything. It's a simple data structure that defines how the program should execute. You can see it has the "blueprint" of how things should work in our app (once executed).

When we call Effect.runPromise a Runtime is then responsible to interpret the Effect, manage its resources, provide services, and execute it.

Effect.runPromise that we used since now is actually an alias for Runtime.runPromise(defaultRuntime).

It uses defaultRuntime which is a basic runtime provided out-of-the-box by effect.