Skip to content

Basic Concurrency

Effect is a highly concurrent framework powered by fibers. Fibers are lightweight virtual threads with resource-safe cancellation capabilities, enabling many features in Effect.

In this section, you will learn the basics of fibers and get familiar with some of the powerful high-level operators that utilize fibers.

JavaScript is inherently single-threaded, meaning it executes code in a single sequence of instructions. However, modern JavaScript environments use an event loop to manage asynchronous operations, creating the illusion of multitasking. In this context, virtual threads, or fibers, are logical threads simulated by the Effect runtime. They allow concurrent execution without relying on true multi-threading, which is not natively supported in JavaScript.

All effects in Effect are executed by fibers. If you didn’t create the fiber yourself, it was created by an operation you’re using (if it’s concurrent) or by the Effect runtime system.

Even if you write “single-threaded” code with no concurrent operations, there will always be at least one fiber: the “main” fiber that executes your effect.

Effect fibers have a well-defined lifecycle based on the effect they are executing.

Every fiber exits with either a failure or success, depending on whether the effect it is executing fails or succeeds.

Effect fibers have unique identities, local state, and a status (such as done, running, or suspended).

The Fiber data type in Effect represents a “handle” on the execution of an effect.

The Fiber<A, E> data type has two type parameters:

  • A (Success Type): The type of value the fiber may succeed with.
  • E (Failure Type): The type of value the fiber may fail with.

Fibers do not have an R type parameter because they only execute effects that have already had their requirements provided to them.

One of the fundamental ways to create a fiber is by forking an existing effect. When you fork an effect, it starts executing the effect on a new fiber, giving you a reference to this newly-created fiber.

The following code demonstrates how to create a single fiber using the Effect.fork function. This fiber will execute the function fib(10) independently of the main fiber:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const fib: (n: number) => Effect.Effect<number>
fib
= (
(parameter) n: number
n
: number):
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
<number> =>
4
import Effect
Effect
.
const suspend: <number, never, never>(effect: LazyArg<Effect.Effect<number, never, never>>) => Effect.Effect<number, never, never>
suspend
(() => {
5
if (
(parameter) n: number
n
<= 1) {
6
return
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(
(parameter) n: number
n
)
7
}
8
return
const fib: (n: number) => Effect.Effect<number>
fib
(
(parameter) n: number
n
- 1).
(method) Pipeable.pipe<Effect.Effect<number, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<number, never, never>) => Effect.Effect<number, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const zipWith: <number, never, never, number, number>(that: Effect.Effect<number, never, never>, f: (a: number, b: number) => number, options?: { readonly concurrent?: boolean | undefined; readonly batching?: boolean | "inherit" | undefined; readonly concurrentFinalizers?: boolean | undefined; }) => <E, R>(self: Effect.Effect<...>) => Effect.Effect<...> (+1 overload)

The `Effect.zipWith` function operates similarly to {@link zip } by combining two effects. However, instead of returning a tuple, it allows you to apply a function to the results of the combined effects, transforming them into a single value

zipWith
(
const fib: (n: number) => Effect.Effect<number>
fib
(
(parameter) n: number
n
- 2), (
(parameter) a: number
a
,
(parameter) b: number
b
) =>
(parameter) a: number
a
+
(parameter) b: number
b
))
9
})
10
11
const
const fib10Fiber: Effect.Effect<RuntimeFiber<number, never>, never, never>
fib10Fiber
=
import Effect
Effect
.
const fork: <number, never, never>(self: Effect.Effect<number, never, never>) => Effect.Effect<RuntimeFiber<number, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
const fib: (n: number) => Effect.Effect<number>
fib
(10))

A common operation with fibers is joining them using the Fiber.join function. This function returns an Effect that will succeed or fail based on the outcome of the fiber it joins:

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
const
const fib: (n: number) => Effect.Effect<number>
fib
= (
(parameter) n: number
n
: number):
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
<number> =>
4
import Effect
Effect
.
const suspend: <number, never, never>(effect: LazyArg<Effect.Effect<number, never, never>>) => Effect.Effect<number, never, never>
suspend
(() => {
5
if (
(parameter) n: number
n
<= 1) {
6
return
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(
(parameter) n: number
n
)
7
}
8
return
const fib: (n: number) => Effect.Effect<number>
fib
(
(parameter) n: number
n
- 1).
(method) Pipeable.pipe<Effect.Effect<number, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<number, never, never>) => Effect.Effect<number, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const zipWith: <number, never, never, number, number>(that: Effect.Effect<number, never, never>, f: (a: number, b: number) => number, options?: { readonly concurrent?: boolean | undefined; readonly batching?: boolean | "inherit" | undefined; readonly concurrentFinalizers?: boolean | undefined; }) => <E, R>(self: Effect.Effect<...>) => Effect.Effect<...> (+1 overload)

The `Effect.zipWith` function operates similarly to {@link zip } by combining two effects. However, instead of returning a tuple, it allows you to apply a function to the results of the combined effects, transforming them into a single value

zipWith
(
const fib: (n: number) => Effect.Effect<number>
fib
(
(parameter) n: number
n
- 2), (
(parameter) a: number
a
,
(parameter) b: number
b
) =>
(parameter) a: number
a
+
(parameter) b: number
b
))
9
})
10
11
const
const fib10Fiber: Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>
fib10Fiber
=
import Effect
Effect
.
const fork: <number, never, never>(self: Effect.Effect<number, never, never>) => Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
const fib: (n: number) => Effect.Effect<number>
fib
(10))
12
13
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>> | YieldWrap<Effect.Effect<number, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
14
const
const fiber: Fiber.RuntimeFiber<number, never>
fiber
= yield*
const fib10Fiber: Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>
fib10Fiber
15
const
const n: number
n
= yield*
import Fiber
Fiber
.
const join: <number, never>(self: Fiber.Fiber<number, never>) => Effect.Effect<number, never, never>

Joins the fiber, which suspends the joining fiber until the result of the fiber has been determined. Attempting to join a fiber that has erred will result in a catchable error. Joining an interrupted fiber will result in an "inner interruption" of this fiber, unlike interruption triggered by another fiber, "inner interruption" can be caught and recovered.

join
(
const fiber: Fiber.RuntimeFiber<number, never>
fiber
)
16
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
(
const n: number
n
)
17
})
18
19
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
) // 55

Another useful function for fibers is Fiber.await. This function returns an effect containing an Exit value, which provides detailed information about how the fiber completed.

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
const
const fib: (n: number) => Effect.Effect<number>
fib
= (
(parameter) n: number
n
: number):
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
<number> =>
4
import Effect
Effect
.
const suspend: <number, never, never>(effect: LazyArg<Effect.Effect<number, never, never>>) => Effect.Effect<number, never, never>
suspend
(() => {
5
if (
(parameter) n: number
n
<= 1) {
6
return
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(
(parameter) n: number
n
)
7
}
8
return
const fib: (n: number) => Effect.Effect<number>
fib
(
(parameter) n: number
n
- 1).
(method) Pipeable.pipe<Effect.Effect<number, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<number, never, never>) => Effect.Effect<number, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const zipWith: <number, never, never, number, number>(that: Effect.Effect<number, never, never>, f: (a: number, b: number) => number, options?: { readonly concurrent?: boolean | undefined; readonly batching?: boolean | "inherit" | undefined; readonly concurrentFinalizers?: boolean | undefined; }) => <E, R>(self: Effect.Effect<...>) => Effect.Effect<...> (+1 overload)

The `Effect.zipWith` function operates similarly to {@link zip } by combining two effects. However, instead of returning a tuple, it allows you to apply a function to the results of the combined effects, transforming them into a single value

zipWith
(
const fib: (n: number) => Effect.Effect<number>
fib
(
(parameter) n: number
n
- 2), (
(parameter) a: number
a
,
(parameter) b: number
b
) =>
(parameter) a: number
a
+
(parameter) b: number
b
))
9
})
10
11
const
const fib10Fiber: Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>
fib10Fiber
=
import Effect
Effect
.
const fork: <number, never, never>(self: Effect.Effect<number, never, never>) => Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
const fib: (n: number) => Effect.Effect<number>
fib
(10))
12
13
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>> | YieldWrap<Effect.Effect<Exit<number, never>, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
14
const
const fiber: Fiber.RuntimeFiber<number, never>
fiber
= yield*
const fib10Fiber: Effect.Effect<Fiber.RuntimeFiber<number, never>, never, never>
fib10Fiber
15
const
const exit: Exit<number, never>
exit
= yield*
import Fiber
Fiber
.
(alias) await<number, never>(self: Fiber.Fiber<number, never>): Effect.Effect<Exit<number, never>, never, never> export await

Awaits the fiber, which suspends the awaiting fiber until the result of the fiber has been determined.

await
(
const fiber: Fiber.RuntimeFiber<number, never>
fiber
)
16
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
(
const exit: Exit<number, never>
exit
)
17
})
18
19
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
) // { _id: 'Exit', _tag: 'Success', value: 55 }

If a fiber’s result is no longer needed, it can be interrupted, which immediately terminates the fiber and safely releases all resources by running all finalizers.

Similar to Fiber.await, Fiber.interrupt returns an Exit value describing how the fiber completed.

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Fiber.RuntimeFiber<never, never>, never, never>> | YieldWrap<Effect.Effect<Exit<never, never>, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const fiber: Fiber.RuntimeFiber<never, never>
fiber
= yield*
import Effect
Effect
.
const fork: <never, never, never>(self: Effect.Effect<never, never, never>) => Effect.Effect<Fiber.RuntimeFiber<never, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const forever: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<never, never, never>

Repeats this effect forever (until the first error).

forever
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("Hi!")))
5
const
const exit: Exit<never, never>
exit
= yield*
import Fiber
Fiber
.
const interrupt: <never, never>(self: Fiber.Fiber<never, never>) => Effect.Effect<Exit<never, never>, never, never>

Interrupts the fiber from whichever fiber is calling this method. If the fiber has already exited, the returned effect will resume immediately. Otherwise, the effect will resume when the fiber exits.

interrupt
(
const fiber: Fiber.RuntimeFiber<never, never>
fiber
)
6
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
(
const exit: Exit<never, never>
exit
)
7
})
8
9
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
)
10
/*
11
Output
12
{
13
_id: 'Exit',
14
_tag: 'Failure',
15
cause: {
16
_id: 'Cause',
17
_tag: 'Interrupt',
18
fiberId: {
19
_id: 'FiberId',
20
_tag: 'Runtime',
21
id: 0,
22
startTimeMillis: 1715787137490
23
}
24
}
25
}
26
*/

By design, the effect returned by Fiber.interrupt does not resume until the fiber has completed, ensuring that your code does not start new fibers until the old one has terminated. This behavior, often called “back-pressuring,” can be overridden if needed.

If you do not need back-pressuring, you can fork the interruption itself into a new fiber:

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Fiber.RuntimeFiber<Exit<never, never>, never>, never, never>>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const fiber: Fiber.RuntimeFiber<never, never>
fiber
= yield*
import Effect
Effect
.
const fork: <never, never, never>(self: Effect.Effect<never, never, never>) => Effect.Effect<Fiber.RuntimeFiber<never, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const forever: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<never, never, never>

Repeats this effect forever (until the first error).

forever
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("Hi!")))
5
const
const _: Fiber.RuntimeFiber<Exit<never, never>, never>
_
= yield*
import Effect
Effect
.
const fork: <Exit<never, never>, never, never>(self: Effect.Effect<Exit<never, never>, never, never>) => Effect.Effect<Fiber.RuntimeFiber<Exit<never, never>, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Fiber
Fiber
.
const interrupt: <never, never>(self: Fiber.Fiber<never, never>) => Effect.Effect<Exit<never, never>, never, never>

Interrupts the fiber from whichever fiber is calling this method. If the fiber has already exited, the returned effect will resume immediately. Otherwise, the effect will resume when the fiber exits.

interrupt
(
const fiber: Fiber.RuntimeFiber<never, never>
fiber
))
6
})

There is also a shorthand for background interruption called Fiber.interruptFork.

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
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* () {
4
const
const fiber: Fiber.RuntimeFiber<never, never>
fiber
= yield*
import Effect
Effect
.
const fork: <never, never, never>(self: Effect.Effect<never, never, never>) => Effect.Effect<Fiber.RuntimeFiber<never, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const forever: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<never, never, never>

Repeats this effect forever (until the first error).

forever
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("Hi!")))
5
const
const _: void
_
= yield*
import Fiber
Fiber
.
const interruptFork: <never, never>(self: Fiber.Fiber<never, never>) => Effect.Effect<void>

Interrupts the fiber from whichever fiber is calling this method. The interruption will happen in a separate daemon fiber, and the returned effect will always resume immediately without waiting.

interruptFork
(
const fiber: Fiber.RuntimeFiber<never, never>
fiber
)
6
})

Note: It is also possible to perform interruptions using the high-level API Effect.interrupt. For more information, see Effect.interrupt.

The Fiber.zip and Fiber.zipWith functions allow you to combine two fibers into a single fiber. The resulting fiber produces the results of both input fibers. If either of the input fibers fails, the composed fiber will also fail.

Here’s an example using Fiber.zip:

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Fiber.RuntimeFiber<string, never>, never, never>> | YieldWrap<Effect.Effect<[string, string], never, never>>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const fiber1: Fiber.RuntimeFiber<string, never>
fiber1
= yield*
import Effect
Effect
.
const fork: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<Fiber.RuntimeFiber<string, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("Hi!"))
5
const
const fiber2: Fiber.RuntimeFiber<string, never>
fiber2
= yield*
import Effect
Effect
.
const fork: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<Fiber.RuntimeFiber<string, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("Bye!"))
6
const
const fiber: Fiber.Fiber<[string, string], never>
fiber
=
import Fiber
Fiber
.
const zip: <string, never, string, never>(self: Fiber.Fiber<string, never>, that: Fiber.Fiber<string, never>) => Fiber.Fiber<[string, string], never> (+1 overload)

Zips this fiber and the specified fiber together, producing a tuple of their output.

zip
(
const fiber1: Fiber.RuntimeFiber<string, never>
fiber1
,
const fiber2: Fiber.RuntimeFiber<string, never>
fiber2
)
7
const
const tuple: [string, string]
tuple
= yield*
import Fiber
Fiber
.
const join: <[string, string], never>(self: Fiber.Fiber<[string, string], never>) => Effect.Effect<[string, string], never, never>

Joins the fiber, which suspends the joining fiber until the result of the fiber has been determined. Attempting to join a fiber that has erred will result in a catchable error. Joining an interrupted fiber will result in an "inner interruption" of this fiber, unlike interruption triggered by another fiber, "inner interruption" can be caught and recovered.

join
(
const fiber: Fiber.Fiber<[string, string], never>
fiber
)
8
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
(
const tuple: [string, string]
tuple
)
9
})
10
11
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
)
12
/*
13
Output:
14
[ 'Hi!', 'Bye!' ]
15
*/

Another way to compose fibers is with the Fiber.orElse function. This function allows you to specify an alternative fiber that will be executed if the first fiber fails. If the first fiber succeeds, the composed fiber will return its result. If the first fiber fails, the composed fiber will complete with the result of the second fiber, regardless of whether it succeeds or fails.

Here’s an example using Fiber.orElse:

1
import {
import Effect
Effect
,
import Fiber
Fiber
} from "effect"
2
3
const
const program: Effect.Effect<void, string, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Fiber.RuntimeFiber<never, string>, never, never>> | YieldWrap<Effect.Effect<Fiber.RuntimeFiber<string, never>, never, never>> | YieldWrap<...>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const fiber1: Fiber.RuntimeFiber<never, string>
fiber1
= yield*
import Effect
Effect
.
const fork: <never, string, never>(self: Effect.Effect<never, string, never>) => Effect.Effect<Fiber.RuntimeFiber<never, string>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("Uh oh!"))
5
const
const fiber2: Fiber.RuntimeFiber<string, never>
fiber2
= yield*
import Effect
Effect
.
const fork: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<Fiber.RuntimeFiber<string, never>, never, never>

Returns an effect that forks this effect into its own separate fiber, returning the fiber immediately, without waiting for it to begin executing the effect. You can use the `fork` method whenever you want to execute an effect in a new fiber, concurrently and without "blocking" the fiber executing other effects. Using fibers can be tricky, so instead of using this method directly, consider other higher-level methods, such as `raceWith`, `zipPar`, and so forth. The fiber returned by this method has methods to interrupt the fiber and to wait for it to finish executing the effect. See `Fiber` for more information. Whenever you use this method to launch a new fiber, the new fiber is attached to the parent fiber's scope. This means when the parent fiber terminates, the child fiber will be terminated as well, ensuring that no fibers leak. This behavior is called "auto supervision", and if this behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.

fork
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("Hurray!"))
6
const
const fiber: Fiber.Fiber<string, string>
fiber
=
import Fiber
Fiber
.
const orElse: <never, string, string, never>(self: Fiber.Fiber<never, string>, that: Fiber.Fiber<string, never>) => Fiber.Fiber<string, string> (+1 overload)

Returns a fiber that prefers `this` fiber, but falls back to the `that` one when `this` one fails. Interrupting the returned fiber will interrupt both fibers, sequentially, from left to right.

orElse
(
const fiber1: Fiber.RuntimeFiber<never, string>
fiber1
,
const fiber2: Fiber.RuntimeFiber<string, never>
fiber2
)
7
const
const message: string
message
= yield*
import Fiber
Fiber
.
const join: <string, string>(self: Fiber.Fiber<string, string>) => Effect.Effect<string, string, never>

Joins the fiber, which suspends the joining fiber until the result of the fiber has been determined. Attempting to join a fiber that has erred will result in a catchable error. Joining an interrupted fiber will result in an "inner interruption" of this fiber, unlike interruption triggered by another fiber, "inner interruption" can be caught and recovered.

join
(
const fiber: Fiber.Fiber<string, string>
fiber
)
8
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
(
const message: string
message
)
9
})
10
11
import Effect
Effect
.
const runPromise: <void, string>(effect: Effect.Effect<void, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<void, string, never>
program
)
12
/*
13
Output:
14
Hurray!
15
*/

Effect provides many functions that accept Concurrency Options to help you identify opportunities to parallelize your code.

For example, the standard Effect.zip function combines two effects sequentially. However, there is also a concurrent version, Effect.zip({_, _, { concurrent: true }), which combines two effects concurrently.

In the following example, we use Effect.zip to run two tasks sequentially. The first task takes 1 second, and the second task takes 2 seconds, resulting in a total duration of approximately 3 seconds:

1
import {
import Effect
Effect
,
import Console
Console
} from "effect"
2
3
const
const task1: Effect.Effect<void, never, never>
task1
=
import Effect
Effect
.
const delay: <void, never, never>(self: Effect.Effect<void, never, never>, duration: DurationInput) => Effect.Effect<void, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("task1"), "1 second")
4
const
const task2: Effect.Effect<void, never, never>
task2
=
import Effect
Effect
.
const delay: <void, never, never>(self: Effect.Effect<void, never, never>, duration: DurationInput) => Effect.Effect<void, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("task2"), "2 seconds")
5
6
const
const program: Effect.Effect<[void, void], never, never>
program
=
import Effect
Effect
.
const zip: <void, never, never, void, never, never>(self: Effect.Effect<void, never, never>, that: Effect.Effect<void, never, never>, options?: { readonly concurrent?: boolean | undefined; readonly batching?: boolean | "inherit" | undefined; readonly concurrentFinalizers?: boolean | undefined; } | undefined) => Effect.Effect<...> (+1 overload)

The `Effect.zip` function allows you to combine two effects into a single effect. This combined effect yields a tuple containing the results of both input effects once they succeed. Note that `Effect.zip` processes effects sequentially: it first completes the effect on the left and then the effect on the right. If you want to run the effects concurrently, you can use the `concurrent` option.

zip
(
const task1: Effect.Effect<void, never, never>
task1
,
const task2: Effect.Effect<void, never, never>
task2
)
7
8
import Effect
Effect
.
const runPromise: <[Duration, [void, void]], never>(effect: Effect.Effect<[Duration, [void, void]], never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
import Effect
Effect
.
const timed: <[void, void], never, never>(self: Effect.Effect<[void, void], never, never>) => Effect.Effect<[Duration, [void, void]], never, never>

Returns a new effect that executes this one and times the execution.

timed
(
const program: Effect.Effect<[void, void], never, never>
program
)).
(method) Promise<[Duration, [void, void]]>.then<void, never>(onfulfilled?: ((value: [Duration, [void, void]]) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(([
(parameter) duration: Duration
duration
]) =>
9
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) globalThis.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
(
var String: StringConstructor (value?: any) => string

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

String
(
(parameter) duration: Duration
duration
))
10
)
11
/*
12
Output:
13
task1
14
task2
15
Duration(3s 5ms 369875ns)
16
*/

In this example, we use the concurrent version of Effect.zip to run two tasks concurrently. The total duration will be approximately equal to the duration of the longest task, which is 2 seconds:

1
import {
import Effect
Effect
,
import Console
Console
} from "effect"
2
3
const
const task1: Effect.Effect<void, never, never>
task1
=
import Effect
Effect
.
const delay: <void, never, never>(self: Effect.Effect<void, never, never>, duration: DurationInput) => Effect.Effect<void, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("task1"), "1 second")
4
const
const task2: Effect.Effect<void, never, never>
task2
=
import Effect
Effect
.
const delay: <void, never, never>(self: Effect.Effect<void, never, never>, duration: DurationInput) => Effect.Effect<void, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("task2"), "2 seconds")
5
6
const
const program: Effect.Effect<[void, void], never, never>
program
=
import Effect
Effect
.
const zip: <void, never, never, void, never, never>(self: Effect.Effect<void, never, never>, that: Effect.Effect<void, never, never>, options?: { readonly concurrent?: boolean | undefined; readonly batching?: boolean | "inherit" | undefined; readonly concurrentFinalizers?: boolean | undefined; } | undefined) => Effect.Effect<...> (+1 overload)

The `Effect.zip` function allows you to combine two effects into a single effect. This combined effect yields a tuple containing the results of both input effects once they succeed. Note that `Effect.zip` processes effects sequentially: it first completes the effect on the left and then the effect on the right. If you want to run the effects concurrently, you can use the `concurrent` option.

zip
(
const task1: Effect.Effect<void, never, never>
task1
,
const task2: Effect.Effect<void, never, never>
task2
, {
(property) concurrent?: boolean | undefined
concurrent
: true })
7
8
import Effect
Effect
.
const runPromise: <[Duration, [void, void]], never>(effect: Effect.Effect<[Duration, [void, void]], never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
import Effect
Effect
.
const timed: <[void, void], never, never>(self: Effect.Effect<[void, void], never, never>) => Effect.Effect<[Duration, [void, void]], never, never>

Returns a new effect that executes this one and times the execution.

timed
(
const program: Effect.Effect<[void, void], never, never>
program
)).
(method) Promise<[Duration, [void, void]]>.then<void, never>(onfulfilled?: ((value: [Duration, [void, void]]) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(([
(parameter) duration: Duration
duration
]) =>
9
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) globalThis.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
(
var String: StringConstructor (value?: any) => string

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

String
(
(parameter) duration: Duration
duration
))
10
)
11
/*
12
Output:
13
task1
14
task2
15
Duration(2s 8ms 179666ns)
16
*/

The Effect.race function lets you race multiple effects concurrently and returns the result of the first one that successfully completes. Here’s an example:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const task1: Effect.Effect<never, string, never>
task1
=
import Effect
Effect
.
const delay: <never, string, never>(self: Effect.Effect<never, string, never>, duration: DurationInput) => Effect.Effect<never, string, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("task1"), "1 second")
4
const
const task2: Effect.Effect<string, never, never>
task2
=
import Effect
Effect
.
const delay: <string, never, never>(self: Effect.Effect<string, never, never>, duration: DurationInput) => Effect.Effect<string, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("task2"), "2 seconds")
5
6
const
const program: Effect.Effect<string, string, never>
program
=
import Effect
Effect
.
const race: <never, string, never, string, never, never>(self: Effect.Effect<never, string, never>, that: Effect.Effect<string, never, never>) => Effect.Effect<string, string, never> (+1 overload)

Returns an effect that races this effect with the specified effect, returning the first successful `A` from the faster side. If one effect succeeds, the other will be interrupted. If neither succeeds, then the effect will fail with some error.

race
(
const task1: Effect.Effect<never, string, never>
task1
,
const task2: Effect.Effect<string, never, never>
task2
)
7
8
import Effect
Effect
.
const runPromise: <string, string>(effect: Effect.Effect<string, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<string>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<string, string, never>
program
).
(method) Promise<string>.then<void, never>(onfulfilled?: ((value: string) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
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
)
9
/*
10
Output:
11
task2
12
*/

In this example, task1 is set to fail after 1 second, while task2 is set to succeed after 2 seconds. The Effect.race function runs both tasks concurrently, and since task2 is the first to succeed, its result is returned.

If you need to handle the first effect to complete, whether it succeeds or fails, you can use the Effect.either function. This function wraps the result in an Either type, allowing you to see if the outcome was a success (Right) or a failure (Left):

1
import {
import Effect
Effect
} from "effect"
2
3
const
const task1: Effect.Effect<never, string, never>
task1
=
import Effect
Effect
.
const delay: <never, string, never>(self: Effect.Effect<never, string, never>, duration: DurationInput) => Effect.Effect<never, string, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Effect
Effect
.
const fail: <string>(error: string) => Effect.Effect<never, string, never>
fail
("task1"), "1 second")
4
const
const task2: Effect.Effect<string, never, never>
task2
=
import Effect
Effect
.
const delay: <string, never, never>(self: Effect.Effect<string, never, never>, duration: DurationInput) => Effect.Effect<string, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("task2"), "2 seconds")
5
6
const
const program: Effect.Effect<Either<never, string> | Either<string, never>, never, never>
program
=
import Effect
Effect
.
const race: <Either<never, string>, never, never, Either<string, never>, never, never>(self: Effect.Effect<Either<never, string>, never, never>, that: Effect.Effect<Either<string, never>, never, never>) => Effect.Effect<...> (+1 overload)

Returns an effect that races this effect with the specified effect, returning the first successful `A` from the faster side. If one effect succeeds, the other will be interrupted. If neither succeeds, then the effect will fail with some error.

race
(
import Effect
Effect
.
const either: <never, string, never>(self: Effect.Effect<never, string, never>) => Effect.Effect<Either<never, string>, never, never>

Returns an effect whose failure and success have been lifted into an `Either`. The resulting effect cannot fail, because the failure case has been exposed as part of the `Either` success case. This method is useful for recovering from effects that may fail. The error parameter of the returned `Effect` is `never`, since it is guaranteed the effect does not model failure.

either
(
const task1: Effect.Effect<never, string, never>
task1
),
import Effect
Effect
.
const either: <string, never, never>(self: Effect.Effect<string, never, never>) => Effect.Effect<Either<string, never>, never, never>

Returns an effect whose failure and success have been lifted into an `Either`. The resulting effect cannot fail, because the failure case has been exposed as part of the `Either` success case. This method is useful for recovering from effects that may fail. The error parameter of the returned `Effect` is `never`, since it is guaranteed the effect does not model failure.

either
(
const task2: Effect.Effect<string, never, never>
task2
))
7
8
import Effect
Effect
.
const runPromise: <Either<never, string> | Either<string, never>, never>(effect: Effect.Effect<Either<never, string> | Either<string, never>, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<Either<never, string> | Either<string, never>, never, never>
program
).
(method) Promise<Either<never, string> | Either<string, never>>.then<void, never>(onfulfilled?: ((value: Either<never, string> | Either<string, never>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
(
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
)
9
/*
10
Output:
11
{ _id: 'Either', _tag: 'Left', left: 'task1' }
12
*/

In this example, task1 fails after 1 second, and task2 succeeds after 2 seconds. By using Effect.either, the program returns the result of task1, showing that it was a failure (Left).

When working with asynchronous tasks, it’s often important to ensure that they complete within a reasonable time. Effect provides a convenient way to enforce time limits on effects using the Effect.timeout function. This function returns a new effect that will fail with a TimeoutException if the original effect does not complete within the specified duration.

Here’s an example demonstrating how to use Effect.timeout:

1
import {
import Effect
Effect
} from "effect"
2
3
const
const task: Effect.Effect<string, never, never>
task
=
import Effect
Effect
.
const delay: <string, never, never>(self: Effect.Effect<string, never, never>, duration: DurationInput) => Effect.Effect<string, never, never> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
(
import Effect
Effect
.
const succeed: <string>(value: string) => Effect.Effect<string, never, never>
succeed
("task1"), "10 seconds")
4
5
const
const program: Effect.Effect<string, TimeoutException, never>
program
=
import Effect
Effect
.
const timeout: <string, never, never>(self: Effect.Effect<string, never, never>, duration: DurationInput) => Effect.Effect<string, TimeoutException, never> (+1 overload)

Returns an effect that will timeout this effect, failing with a `Cause.TimeoutException` if the timeout elapses before the effect has produced a value. If the timeout elapses without producing a value, the running effect will be safely interrupted. WARNING: The effect returned by this method will not itself return until the underlying effect is actually interrupted. This leads to more predictable resource utilization. If early return is desired, then instead of using `effect.timeout(d)`, use `effect.disconnect.timeout(d)`, which first disconnects the effect's interruption signal before performing the timeout, resulting in earliest possible return, before an underlying effect has been successfully interrupted.

timeout
(
const task: Effect.Effect<string, never, never>
task
, "2 seconds")
6
7
import Effect
Effect
.
const runPromise: <string, TimeoutException>(effect: Effect.Effect<string, TimeoutException, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<...>

Runs an `Effect` workflow, returning a `Promise` which resolves with the result of the workflow or rejects with an error.

runPromise
(
const program: Effect.Effect<string, TimeoutException, never>
program
)
8
/*
9
throws:
10
TimeoutException
11
*/

In this example, task is an effect that succeeds after 10 seconds. By wrapping task with Effect.timeout and specifying a timeout of 2 seconds, the resulting program will fail with a TimeoutException because the task takes longer than the allowed time.

If an effect times out, the effect library automatically interrupts it to prevent it from continuing to execute in the background. This interruption ensures efficient use of resources by stopping unnecessary work.