Skip to content

Introduction to Runtime

The Runtime<R> data type represents a runtime system that can execute effects. To run an effect, Effect<A, E, R>, we need a Runtime<R> that contains the required resources, denoted by the R type parameter.

A Runtime<R> consists of three main components:

  • A value of type Context<R>
  • A value of type FiberRefs
  • A value of type RuntimeFlags

When we write an Effect program, we construct an Effect using constructors and combinators. Essentially, we are creating a blueprint of a program. An Effect is merely a data structure that describes the execution of a concurrent program. It represents a tree-like structure that combines various primitives to define what the effect should do.

However, this data structure itself does not perform any actions, it is solely a description of a concurrent program.

To execute this program, the Effect runtime system comes into play. The Runtime.run* functions (e.g., Runtime.runPromise, Runtime.runFork) are responsible for taking this blueprint and executing it.

When the runtime system runs an effect, it creates a root fiber, initializing it with:

  • The initial context
  • The initial FiberRefs
  • The initial effect

It then starts a loop, executing the instructions described by the Effect step by step.

You can think of the runtime as a system that takes an Effect<A, E, R> and its associated context Context<R> and produces an Exit<A, E> result.

┌────────────────────────────────┐
│ Context<R> + Effect<A, E, R> │
└────────────────────────────────┘
┌────────────────────────────────┐
│ Effect Runtime System │
└────────────────────────────────┘
┌────────────────────────────────┐
│ Exit<A, E> │
└────────────────────────────────┘

Runtime Systems have a lot of responsibilities:

ResponsibilityDescription
Executing the programThe runtime must execute every step of the effect in a loop until the program completes.
Handling errorsIt handles both expected and unexpected errors that occur during execution.
Managing concurrencyThe runtime spawns new fibers when Effect.fork is called to handle concurrent operations.
Cooperative yieldingIt ensures fibers don’t monopolize resources, yielding control when necessary.
Ensuring resource cleanupThe runtime guarantees finalizers run properly to clean up resources when needed.
Handling async callbacksThe runtime deals with asynchronous operations transparently, allowing you to write async and sync code uniformly.

When we use functions that run effects like Effect.runPromise or Effect.runFork, we are actually using the default runtime without explicitly mentioning it. These functions are designed as convenient shortcuts for executing our effects using the default runtime.

Each of the Effect.run* functions internally calls the corresponding Runtime.run* function, passing in the default runtime. For example, Effect.runPromise is just an alias for Runtime.runPromise(defaultRuntime).

Both of the following executions are functionally equivalent:

Example (Running an Effect Using the Default Runtime)

1
import {
import Effect
Effect
,
import Runtime
Runtime
} from "effect"
2
3
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("Application started!")
4
5
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
)
6
/*
7
Output:
8
timestamp=... level=INFO fiber=#0 message="Application started!"
9
*/
10
11
import Runtime
Runtime
.
const runPromise: <never>(runtime: Runtime.Runtime<never>) => <A, E>(effect: Effect.Effect<A, E, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Runs the `Effect`, returning a JavaScript `Promise` that will be resolved with the value of the effect once the effect has been executed, or will be rejected with the first error or exception throw by the effect. This method is effectful and should only be used at the edges of your program.

runPromise
(
import Runtime
Runtime
.
const defaultRuntime: Runtime.Runtime<never>
defaultRuntime
)(
const program: Effect.Effect<void, never, never>
program
)
12
/*
13
Output:
14
timestamp=... level=INFO fiber=#0 message="Application started!"
15
*/

In both cases, the program runs using the default runtime, producing the same output.

The default runtime includes:

  • An empty context
  • A set of FiberRefs that include the default services
  • A default configuration for RuntimeFlags that enables Interruption and CooperativeYielding

In most scenarios, using the default runtime is sufficient for effect execution. However, there are cases where it’s helpful to create a custom runtime, particularly when you need to reuse specific configurations or contexts.

For example, in a React app or when executing operations on a server in response to API requests, you might create a Runtime<R> by initializing a layer Layer<R, Err, RIn>. This allows you to maintain a consistent context across different execution boundaries.

In Effect, runtime configurations are typically inherited from their parent workflows. This means that when we access a runtime configuration or obtain a runtime inside a workflow, we are essentially using the configuration of the parent workflow.

However, there are cases where we want to temporarily override the runtime configuration for a specific part of our code. This concept is known as locally scoped runtime configuration. Once the execution of that code region is completed, the runtime configuration reverts to its original settings.

To achieve this, we make use of the Effect.provide function, which allow us to provide a new runtime configuration to a specific section of our code.

Example (Overriding the Logger Configuration)

In this example, we create a simple logger using Logger.replace, which replaces the default logger with a custom one that logs messages without timestamps or levels. We then use Effect.provide to apply this custom logger to the program.

1
import {
import Logger
Logger
,
import Effect
Effect
} from "effect"
2
3
const
const addSimpleLogger: Layer<never, never, never>
addSimpleLogger
=
import Logger
Logger
.
const replace: <void, void>(self: Logger.Logger<unknown, void>, that: Logger.Logger<unknown, void>) => Layer<never> (+1 overload)
replace
(
4
import Logger
Logger
.
const defaultLogger: Logger.Logger<unknown, void>
defaultLogger
,
5
// Custom logger implementation
6
import Logger
Logger
.
const make: <unknown, void>(log: (options: Logger.Logger<in Message, out Output>.Options<unknown>) => void) => Logger.Logger<unknown, void>

Creates a custom logger that formats log messages according to the provided function.

make
(({
(parameter) message: unknown
message
}) =>
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
(parameter) message: unknown
message
))
7
)
8
9
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<void, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<void, never, never>>, void, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
10
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("Application started!")
11
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("Application is about to exit!")
12
})
13
14
// Running with the default logger
15
import Effect
Effect
.
const runFork: <void, never>(effect: Effect.Effect<void, never, never>, options?: RunForkOptions) => RuntimeFiber<void, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const program: Effect.Effect<void, never, never>
program
)
16
/*
17
Output:
18
timestamp=... level=INFO fiber=#0 message="Application started!"
19
timestamp=... level=INFO fiber=#0 message="Application is about to exit!"
20
*/
21
22
// Overriding the default logger with a custom one
23
import Effect
Effect
.
const runFork: <void, never>(effect: Effect.Effect<void, never, never>, options?: RunForkOptions) => RuntimeFiber<void, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const program: Effect.Effect<void, never, never>
program
.
(method) Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<void, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<void, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const provide: <never, never, never>(layer: Layer<never, never, never>) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, never>> (+9 overloads)

Splits the context into two parts, providing one part using the specified layer/context/runtime and leaving the remainder `R0`

provide
(
const addSimpleLogger: Layer<never, never, never>
addSimpleLogger
)))
24
/*
25
Output:
26
[ 'Application started!' ]
27
[ 'Application is about to exit!' ]
28
*/

To ensure that the runtime configuration is only applied to a specific part of an Effect application, we should provide the configuration layer exclusively to that particular section.

Example (Providing a configuration layer to a nested workflow)

In this example, we demonstrate how to apply a custom logger configuration only to a specific section of the program. The default logger is used for most of the program, but when we apply the Effect.provide(addSimpleLogger) call, it overrides the logger within that specific nested block. After that, the configuration reverts to its original state.

1
import {
import Logger
Logger
,
import Effect
Effect
} from "effect"
2
3
const
const addSimpleLogger: Layer<never, never, never>
addSimpleLogger
=
import Logger
Logger
.
const replace: <void, void>(self: Logger.Logger<unknown, void>, that: Logger.Logger<unknown, void>) => Layer<never> (+1 overload)
replace
(
4
import Logger
Logger
.
const defaultLogger: Logger.Logger<unknown, void>
defaultLogger
,
5
// Custom logger implementation
6
import Logger
Logger
.
const make: <unknown, void>(log: (options: Logger.Logger<in Message, out Output>.Options<unknown>) => void) => Logger.Logger<unknown, void>

Creates a custom logger that formats log messages according to the provided function.

make
(({
(parameter) message: unknown
message
}) =>
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
(parameter) message: unknown
message
))
7
)
8
9
const
const removeDefaultLogger: Layer<never, never, never>
removeDefaultLogger
=
import Logger
Logger
.
const remove: <void>(logger: Logger.Logger<unknown, void>) => Layer<never>
remove
(
import Logger
Logger
.
const defaultLogger: Logger.Logger<unknown, void>
defaultLogger
)
10
11
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<void, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<void, never, never>>, void, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
12
// Logs with default logger
13
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("Application started!")
14
15
yield*
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<void, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<void, never, never>>, void, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
16
// This log is suppressed
17
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("I'm not going to be logged!")
18
19
// Custom logger applied here
20
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("I will be logged by the simple logger.").
(method) Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<void, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<void, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
21
import Effect
Effect
.
const provide: <never, never, never>(layer: Layer<never, never, never>) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, never>> (+9 overloads)

Splits the context into two parts, providing one part using the specified layer/context/runtime and leaving the remainder `R0`

provide
(
const addSimpleLogger: Layer<never, never, never>
addSimpleLogger
)
22
)
23
24
// This log is suppressed
25
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
(
26
"Reset back to the previous configuration, so I won't be logged."
27
)
28
}).
(method) Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<void, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<void, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
29
// Remove the default logger temporarily
30
import Effect
Effect
.
const provide: <never, never, never>(layer: Layer<never, never, never>) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, never>> (+9 overloads)

Splits the context into two parts, providing one part using the specified layer/context/runtime and leaving the remainder `R0`

provide
(
const removeDefaultLogger: Layer<never, never, never>
removeDefaultLogger
)
31
)
32
33
// Logs with default logger again
34
yield*
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("Application is about to exit!")
35
})
36
37
import Effect
Effect
.
const runSync: <void, never>(effect: Effect.Effect<void, never, never>) => void

Executes an effect synchronously and returns its result. Use `runSync` when you are certain that the effect is purely synchronous and will not perform any asynchronous operations. If the effect fails or contains asynchronous tasks, it will throw an error.

runSync
(
const program: Effect.Effect<void, never, never>
program
)
38
/*
39
Output:
40
timestamp=... level=INFO fiber=#0 message="Application started!"
41
[ 'I will be logged by the simple logger.' ]
42
timestamp=... level=INFO fiber=#0 message="Application is about to exit!"
43
*/

When developing an Effect application and using Effect.run* functions to execute it, the application is automatically run using the default runtime behind the scenes. While it’s possible to adjust specific parts of the application by providing locally scoped configuration layers using Effect.provide, there are scenarios where you might want to customize the runtime configuration for the entire application from the top level.

In these cases, you can create a top-level runtime by converting a configuration layer into a runtime using the ManagedRuntime.make constructor.

Example (Creating and Using a Custom Managed Runtime)

In this example, we first create a custom configuration layer called appLayer, which replaces the default logger with a simple one that logs messages to the console. Next, we use ManagedRuntime.make to turn this configuration layer into a runtime.

1
import {
import Effect
Effect
,
import ManagedRuntime
ManagedRuntime
,
import Logger
Logger
} from "effect"
2
3
// Define a configuration layer that replaces the default logger
4
const
const appLayer: Layer<never, never, never>
appLayer
=
import Logger
Logger
.
const replace: <void, void>(self: Logger.Logger<unknown, void>, that: Logger.Logger<unknown, void>) => Layer<never> (+1 overload)
replace
(
5
import Logger
Logger
.
const defaultLogger: Logger.Logger<unknown, void>
defaultLogger
,
6
// Custom logger implementation
7
import Logger
Logger
.
const make: <unknown, void>(log: (options: Logger.Logger<in Message, out Output>.Options<unknown>) => void) => Logger.Logger<unknown, void>

Creates a custom logger that formats log messages according to the provided function.

make
(({
(parameter) message: unknown
message
}) =>
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
(parameter) message: unknown
message
))
8
)
9
10
// Create a custom runtime from the configuration layer
11
const
const runtime: ManagedRuntime.ManagedRuntime<never, never>
runtime
=
import ManagedRuntime
ManagedRuntime
.
const make: <never, never>(layer: Layer<never, never, never>, memoMap?: MemoMap | undefined) => ManagedRuntime.ManagedRuntime<never, never>

Convert a Layer into an ManagedRuntime, that can be used to run Effect's using your services.

make
(
const appLayer: Layer<never, never, never>
appLayer
)
12
13
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const log: (...message: ReadonlyArray<any>) => Effect.Effect<void, never, never>

Logs one or more messages or error causes at the current log level, which is INFO by default. This function allows logging multiple items at once and can include detailed error information using `Cause` instances. To adjust the log level, use the `Logger.withMinimumLogLevel` function.

log
("Application started!")
14
15
// Execute the program using the custom runtime
16
const runtime: ManagedRuntime.ManagedRuntime<never, never>
runtime
.
(property) ManagedRuntime<never, never>.runSync: <void, never>(effect: Effect.Effect<void, never, never>) => void

Executes the effect synchronously throwing in case of errors or async boundaries. This method is effectful and should only be invoked at the edges of your program.

runSync
(
const program: Effect.Effect<void, never, never>
program
)
17
18
// Clean up resources associated with the custom runtime
19
import Effect
Effect
.
const runFork: <void, never>(effect: Effect.Effect<void, never, never>, options?: RunForkOptions) => RuntimeFiber<void, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const runtime: ManagedRuntime.ManagedRuntime<never, never>
runtime
.
(property) ManagedRuntime<never, never>.disposeEffect: Effect.Effect<void, never, never>

Dispose of the resources associated with the runtime.

disposeEffect
)
20
/*
21
Output:
22
[ 'Application started!' ]
23
*/

When working with runtimes that you pass around, Effect.Tag can help simplify the access to services. It lets you define a new tag and embed the service shape directly into the static properties of the tag class.

Example (Defining a Tag for Notifications)

1
import {
import Effect
Effect
} from "effect"
2
3
class
class Notifications
Notifications
extends
import Effect
Effect
.
const Tag: <"Notifications">(id: "Notifications") => <Self, Type>() => TagClass<Self, "Notifications", Type> & (Type extends Record<PropertyKey, any> ? Effect.Tag.Proxy<...> : {}) & { ...; } namespace Tag
Tag
("Notifications")<
4
class Notifications
Notifications
,
5
{ readonly
(property) notify: (message: string) => Effect.Effect<void>
notify
: (
(parameter) message: string
message
: string) =>
import Effect
Effect
.
interface Effect<out A, out E = never, out R = never> namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
<void> }
6
>() {}

In this setup, the fields of the service (in this case, the notify method) are turned into static properties of the Notifications class, making it easier to access them.

This allows you to interact with the service directly:

Example (Using the Notifications Tag)

1
import {
import Effect
Effect
} from "effect"
2
3
class
class Notifications
Notifications
extends
import Effect
Effect
.
const Tag: <"Notifications">(id: "Notifications") => <Self, Type>() => TagClass<Self, "Notifications", Type> & (Type extends Record<PropertyKey, any> ? Effect.Tag.Proxy<...> : {}) & { ...; } namespace Tag
Tag
("Notifications")<
4
class Notifications
Notifications
,
5
{ readonly
(property) notify: (message: string) => Effect.Effect<void>
notify
: (
(parameter) message: string
message
: string) =>
import Effect
Effect
.
interface Effect<out A, out E = never, out R = never> namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
<void> }
6
>() {}
7
8
// Create an effect that depends on the Notifications service
9
const
const action: Effect.Effect<void, never, Notifications>
action
=
class Notifications
Notifications
.
(property) notify: (message: string) => Effect.Effect<void, never, Notifications>
notify
("Hello, world!")
10
// ^? const action: Effect<void, never, Notifications>

In this example, the action effect depends on the Notifications service. This approach allows you to reference services without manually passing them around. Later, you can create a Layer that provides the Notifications service and build a ManagedRuntime with that layer to ensure the service is available where needed.

The ManagedRuntime simplifies the integration of services and layers with other frameworks or tools, particularly in environments where Effect is not the primary framework and access to the main entry point is restricted.

For example, in environments like React or other frameworks where you have limited control over the main application entry point, ManagedRuntime helps manage the lifecycle of services.

Here’s how to manage a service’s lifecycle within an external framework:

Example (Using ManagedRuntime in an External Framework)

1
import {
import Effect
Effect
,
import ManagedRuntime
ManagedRuntime
,
import Layer
Layer
,
import Console
Console
} from "effect"
2
3
// Define the Notifications service using Effect.Tag
4
class
class Notifications
Notifications
extends
import Effect
Effect
.
const Tag: <"Notifications">(id: "Notifications") => <Self, Type>() => TagClass<Self, "Notifications", Type> & (Type extends Record<PropertyKey, any> ? Effect.Tag.Proxy<...> : {}) & { ...; } namespace Tag
Tag
("Notifications")<
5
class Notifications
Notifications
,
6
{ readonly
(property) notify: (message: string) => Effect.Effect<void>
notify
: (
(parameter) message: string
message
: string) =>
import Effect
Effect
.
interface Effect<out A, out E = never, out R = never> namespace Effect

The `Effect` interface defines a value that lazily describes a workflow or job. The workflow requires some context `R`, and may fail with an error of type `E`, or succeed with a value of type `A`. `Effect` values model resourceful interaction with the outside world, including synchronous, asynchronous, concurrent, and parallel interaction. They use a fiber-based concurrency model, with built-in support for scheduling, fine-grained interruption, structured concurrency, and high scalability. To run an `Effect` value, you need a `Runtime`, which is a type that is capable of executing `Effect` values.

Effect
<void> }
7
>() {
8
// Provide a live implementation of the Notifications service
9
static
(property) Notifications.Live: Layer.Layer<Notifications, never, never>
Live
=
import Layer
Layer
.
const succeed: <typeof Notifications>(tag: typeof Notifications, resource: { readonly notify: (message: string) => Effect.Effect<void>; }) => Layer.Layer<Notifications, never, never> (+1 overload)

Constructs a layer from the specified value.

succeed
(this, {
10
(property) notify: (message: string) => Effect.Effect<void>
notify
: (
(parameter) message: string
message
) =>
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
(
(parameter) message: string
message
)
11
})
12
}
13
14
// Example entry point for an external framework
15
async function
function main(): Promise<void>
main
() {
16
// Create a custom runtime using the Notifications layer
17
const
const runtime: ManagedRuntime.ManagedRuntime<Notifications, never>
runtime
=
import ManagedRuntime
ManagedRuntime
.
const make: <Notifications, never>(layer: Layer.Layer<Notifications, never, never>, memoMap?: Layer.MemoMap | undefined) => ManagedRuntime.ManagedRuntime<Notifications, never>

Convert a Layer into an ManagedRuntime, that can be used to run Effect's using your services.

make
(
class Notifications
Notifications
.
(property) Notifications.Live: Layer.Layer<Notifications, never, never>
Live
)
18
19
// Run the effect
20
await
const runtime: ManagedRuntime.ManagedRuntime<Notifications, never>
runtime
.
(property) ManagedRuntime<Notifications, never>.runPromise: <void, never>(effect: Effect.Effect<void, never, Notifications>, options?: { readonly signal?: AbortSignal | undefined; } | undefined) => Promise<void>

Runs the `Effect`, returning a JavaScript `Promise` that will be resolved with the value of the effect once the effect has been executed, or will be rejected with the first error or exception throw by the effect. This method is effectful and should only be used at the edges of your program.

runPromise
(
class Notifications
Notifications
.
(property) notify: (message: string) => Effect.Effect<void, never, Notifications>
notify
("Hello, world!"))
21
22
// Dispose of the runtime, cleaning up resources
23
await
const runtime: ManagedRuntime.ManagedRuntime<Notifications, never>
runtime
.
(property) ManagedRuntime<Notifications, never>.dispose: () => Promise<void>

Dispose of the resources associated with the runtime.

dispose
()
24
}