Skip to content

Deferred

A Deferred<A, E> is a special subtype of Effect that acts as a variable, but with some unique characteristics. It can only be set once, making it a powerful synchronization tool for managing asynchronous operations.

A Deferred is essentially a synchronization primitive that represents a value that may not be available immediately. When you create a Deferred, it starts with an empty value. Later on, you can complete it exactly once with either a success value (A) or a failure value (E). Once completed, a Deferred can never be modified or emptied again.

Deferred becomes incredibly useful when you need to wait for something specific to happen in your program. It’s ideal for scenarios where you want one part of your code to signal another part when it’s ready. Here are a few common use cases:

  • Coordinating Fibers: When you have multiple concurrent tasks (fibers) and need to coordinate their actions, Deferred can help one fiber signal to another when it has completed its task.

  • Synchronization: Anytime you want to ensure that one piece of code doesn’t proceed until another piece of code has finished its work, Deferred can provide the synchronization you need.

  • Handing Over Work: You can use Deferred to hand over work from one fiber to another. For example, one fiber can prepare some data, and then a second fiber can continue processing it.

  • Suspending Execution: When you want a fiber to pause its execution until some condition is met, a Deferred can be used to block it until the condition is satisfied.

When a fiber calls await on a Deferred, it essentially blocks until that Deferred is completed with either a value or an error. Importantly, in Effect, blocking fibers don’t actually block the main thread; they block only semantically. While one fiber is blocked, the underlying thread can execute other fibers, ensuring efficient concurrency.

A Deferred in Effect is conceptually similar to JavaScript’s Promise. The key difference is that Deferred has two type parameters (E and A) instead of just one. This allows Deferred to represent both successful results (A) and errors (E).

You can create a Deferred using Deferred.make<A, E>(). This returns an Effect<Deferred<A, E>>, which describes the creation of a Deferred. Note that Deferreds can only be created within an Effect because creating them involves effectful memory allocation, which must be managed safely within an Effect.

To retrieve a value from a Deferred, you can use Deferred.await. This operation suspends the calling fiber until the Deferred is completed with a value or an error.

1
import {
import Effect
Effect
,
import Deferred
Deferred
} from "effect"
2
3
const
const effectDeferred: Effect.Effect<Deferred.Deferred<string, Error>, never, never>
effectDeferred
=
import Deferred
Deferred
.
const make: <string, Error>() => Effect.Effect<Deferred.Deferred<string, Error>, never, never>

Creates a new `Deferred`.

make
<string,
interface Error
Error
>()
4
5
const
const effectGet: Effect.Effect<string, Error, never>
effectGet
=
const effectDeferred: Effect.Effect<Deferred.Deferred<string, Error>, never, never>
effectDeferred
.
(method) Pipeable.pipe<Effect.Effect<Deferred.Deferred<string, Error>, never, never>, Effect.Effect<string, Error, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<Deferred.Deferred<string, Error>, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
6
import Effect
Effect
.
const andThen: <Deferred.Deferred<string, Error>, Effect.Effect<string, Error, never>>(f: (a: Deferred.Deferred<string, Error>) => Effect.Effect<string, Error, never>) => <E, R>(self: Effect.Effect<...>) => Effect.Effect<...> (+3 overloads)

Executes a sequence of two actions, typically two `Effect`s, where the second action can depend on the result of the first action. The `that` action can take various forms: - a value - a function returning a value - a promise - a function returning a promise - an effect - a function returning an effect

andThen
((
(parameter) deferred: Deferred.Deferred<string, Error>
deferred
) =>
import Deferred
Deferred
.
(alias) await<string, Error>(self: Deferred.Deferred<string, Error>): Effect.Effect<string, Error, never> export await

Retrieves the value of the `Deferred`, suspending the fiber running the workflow until the result is available.

await
(
(parameter) deferred: Deferred.Deferred<string, Error>
deferred
))
7
)

You can complete a Deferred<A, E> in different ways:

There are several ways to complete a Deferred:

  • Deferred.succeed: Completes the Deferred successfully with a value of type A.
  • Deferred.done: Completes the Deferred with an Exit<A, E> type.
  • Deferred.complete: Completes the Deferred with the result of an effect Effect<A, E>.
  • Deferred.completeWith: Completes the Deferred with an effect Effect<A, E>. This effect will be executed by each waiting fiber, so use it carefully.
  • Deferred.fail: Fails the Deferred with an error of type E.
  • Deferred.die: Defects the Deferred with a user-defined error.
  • Deferred.failCause: Fails or defects the Deferred with a Cause<E>.
  • Deferred.interrupt: Interrupts the Deferred. This can be used to forcefully stop or interrupt the waiting fibers.

Here’s an example that demonstrates the usage of these completion methods:

1
import {
import Effect
Effect
,
import Deferred
Deferred
,
import Exit
Exit
,
import Cause
Cause
} from "effect"
2
3
const
const program: Effect.Effect<number, string, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Deferred.Deferred<number, string>, never, never>> | YieldWrap<Effect.Effect<number, string, never>> | YieldWrap<...>, number>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const deferred: Deferred.Deferred<number, string>
deferred
= yield*
import Deferred
Deferred
.
const make: <number, string>() => Effect.Effect<Deferred.Deferred<number, string>, never, never>

Creates a new `Deferred`.

make
<number, string>()
5
6
// Completing the Deferred in various ways
7
yield*
import Deferred
Deferred
.
const succeed: <number, string>(self: Deferred.Deferred<number, string>, value: number) => Effect.Effect<boolean> (+1 overload)

Completes the `Deferred` with the specified value.

succeed
(
const deferred: Deferred.Deferred<number, string>
deferred
, 1).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
)
8
yield*
import Deferred
Deferred
.
const complete: <number, string>(self: Deferred.Deferred<number, string>, effect: Effect.Effect<number, string, never>) => Effect.Effect<boolean> (+1 overload)

Completes the deferred with the result of the specified effect. If the deferred has already been completed, the method will produce false. Note that `Deferred.completeWith` will be much faster, so consider using that if you do not need to memoize the result of the specified effect.

complete
(
const deferred: Deferred.Deferred<number, string>
deferred
,
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(2)).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
)
9
yield*
import Deferred
Deferred
.
const completeWith: <number, string>(self: Deferred.Deferred<number, string>, effect: Effect.Effect<number, string, never>) => Effect.Effect<boolean> (+1 overload)

Completes the deferred with the result of the specified effect. If the deferred has already been completed, the method will produce false.

completeWith
(
const deferred: Deferred.Deferred<number, string>
deferred
,
import Effect
Effect
.
const succeed: <number>(value: number) => Effect.Effect<number, never, never>
succeed
(3)).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
10
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
11
)
12
yield*
import Deferred
Deferred
.
const done: <number, string>(self: Deferred.Deferred<number, string>, exit: Exit.Exit<number, string>) => Effect.Effect<boolean> (+1 overload)

Exits the `Deferred` with the specified `Exit` value, which will be propagated to all fibers waiting on the value of the `Deferred`.

done
(
const deferred: Deferred.Deferred<number, string>
deferred
,
import Exit
Exit
.
const succeed: <number>(value: number) => Exit.Exit<number, never>

Constructs a new `Exit.Success` containing the specified value of type `A`.

succeed
(4)).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
)
13
yield*
import Deferred
Deferred
.
const fail: <number, string>(self: Deferred.Deferred<number, string>, error: string) => Effect.Effect<boolean> (+1 overload)

Fails the `Deferred` with the specified error, which will be propagated to all fibers waiting on the value of the `Deferred`.

fail
(
const deferred: Deferred.Deferred<number, string>
deferred
, "5").
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
)
14
yield*
import Deferred
Deferred
.
const failCause: <number, string>(self: Deferred.Deferred<number, string>, cause: Cause.Cause<string>) => Effect.Effect<boolean> (+1 overload)

Fails the `Deferred` with the specified `Cause`, which will be propagated to all fibers waiting on the value of the `Deferred`.

failCause
(
const deferred: Deferred.Deferred<number, string>
deferred
,
import Cause
Cause
.
const die: (defect: unknown) => Cause.Cause<never>

Constructs a new `Die` cause from the specified `defect`.

die
(new
var Error: ErrorConstructor new (message?: string) => Error
Error
("6"))).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
15
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
16
)
17
yield*
import Deferred
Deferred
.
const die: <number, string>(self: Deferred.Deferred<number, string>, defect: unknown) => Effect.Effect<boolean> (+1 overload)

Kills the `Deferred` with the specified defect, which will be propagated to all fibers waiting on the value of the `Deferred`.

die
(
const deferred: Deferred.Deferred<number, string>
deferred
, new
var Error: ErrorConstructor new (message?: string) => Error
Error
("7")).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
)
18
yield*
import Deferred
Deferred
.
const interrupt: <number, string>(self: Deferred.Deferred<number, string>) => Effect.Effect<boolean>

Completes the `Deferred` with interruption. This will interrupt all fibers waiting on the value of the `Deferred` with the `FiberId` of the fiber calling this method.

interrupt
(
const deferred: Deferred.Deferred<number, string>
deferred
).
(method) Pipeable.pipe<Effect.Effect<boolean, never, never>, Effect.Effect<RuntimeFiber<boolean, never>, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<boolean, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const fork: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<RuntimeFiber<A, E>, never, R>

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
)
19
20
// Awaiting the Deferred to get its value
21
const
const value: number
value
= yield*
import Deferred
Deferred
.
(alias) await<number, string>(self: Deferred.Deferred<number, string>): Effect.Effect<number, string, never> export await

Retrieves the value of the `Deferred`, suspending the fiber running the workflow until the result is available.

await
(
const deferred: Deferred.Deferred<number, string>
deferred
)
22
return
const value: number
value
23
})
24
25
import Effect
Effect
.
const runPromise: <number, string>(effect: Effect.Effect<number, string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<number>

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<number, string, never>
program
).
(method) Promise<number>.then<void, void>(onfulfilled?: ((value: number) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => void | PromiseLike<void>) | 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
,
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.error(message?: any, ...optionalParams: any[]): void

Prints to `stderr` 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 code = 5; console.error('error #%d', code); // Prints: error #5, to stderr console.error('error', code); // Prints: error 5, to stderr ``` If formatting elements (e.g. `%d`) are not found in the first string then [`util.inspect()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilinspectobject-options) is called on each argument and the resulting string values are concatenated. See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

error
) // Output: 1

When you complete a Deferred, it results in an Effect<boolean>. This effect returns true if the Deferred value has been set and false if it was already set before completion. This can be useful for checking the state of the Deferred.

Here’s an example demonstrating the state change of a Deferred:

1
import {
import Effect
Effect
,
import Deferred
Deferred
} from "effect"
2
3
const
const program: Effect.Effect<boolean[], never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Deferred.Deferred<number, string>, never, never>> | YieldWrap<Effect.Effect<boolean, never, never>>, boolean[]>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const deferred: Deferred.Deferred<number, string>
deferred
= yield*
import Deferred
Deferred
.
const make: <number, string>() => Effect.Effect<Deferred.Deferred<number, string>, never, never>

Creates a new `Deferred`.

make
<number, string>()
5
const
const b1: boolean
b1
= yield*
import Deferred
Deferred
.
const fail: <number, string>(self: Deferred.Deferred<number, string>, error: string) => Effect.Effect<boolean> (+1 overload)

Fails the `Deferred` with the specified error, which will be propagated to all fibers waiting on the value of the `Deferred`.

fail
(
const deferred: Deferred.Deferred<number, string>
deferred
, "oh no!")
6
const
const b2: boolean
b2
= yield*
import Deferred
Deferred
.
const succeed: <number, string>(self: Deferred.Deferred<number, string>, value: number) => Effect.Effect<boolean> (+1 overload)

Completes the `Deferred` with the specified value.

succeed
(
const deferred: Deferred.Deferred<number, string>
deferred
, 1)
7
return [
const b1: boolean
b1
,
const b2: boolean
b2
]
8
})
9
10
import Effect
Effect
.
const runPromise: <boolean[], never>(effect: Effect.Effect<boolean[], never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<boolean[]>

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<boolean[], never, never>
program
).
(method) Promise<boolean[]>.then<void, never>(onfulfilled?: ((value: boolean[]) => 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
) // Output: [ true, false ]

Sometimes, you may want to check whether a Deferred has been completed without causing the fiber to suspend. To achieve this, you can use the Deferred.poll method. Here’s how it works:

  • Deferred.poll returns an Option<Effect<A, E>>.
    • If the Deferred is not yet completed, it returns None.
    • If the Deferred is completed, it returns Some, which contains the result or error.

Additionally, you can use the Deferred.isDone method, which returns an Effect<boolean>. This effect evaluates to true if the Deferred is already completed, allowing you to quickly check the completion status.

Here’s a practical example:

1
import {
import Effect
Effect
,
import Deferred
Deferred
} from "effect"
2
3
const
const program: Effect.Effect<(boolean | Option<Effect.Effect<number, string, never>>)[], never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Deferred.Deferred<number, string>, never, never>> | YieldWrap<Effect.Effect<Option<Effect.Effect<number, string, never>>, never, never>> | YieldWrap<...>, (boolean | Option<...>)[]>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
4
const
const deferred: Deferred.Deferred<number, string>
deferred
= yield*
import Deferred
Deferred
.
const make: <number, string>() => Effect.Effect<Deferred.Deferred<number, string>, never, never>

Creates a new `Deferred`.

make
<number, string>()
5
6
// Polling the Deferred
7
const
const done1: Option<Effect.Effect<number, string, never>>
done1
= yield*
import Deferred
Deferred
.
const poll: <number, string>(self: Deferred.Deferred<number, string>) => Effect.Effect<Option<Effect.Effect<number, string, never>>, never, never>

Returns a `Some<Effect<A, E, R>>` from the `Deferred` if this `Deferred` has already been completed, `None` otherwise.

poll
(
const deferred: Deferred.Deferred<number, string>
deferred
)
8
9
// Checking if the Deferred is already completed
10
const
const done2: boolean
done2
= yield*
import Deferred
Deferred
.
const isDone: <number, string>(self: Deferred.Deferred<number, string>) => Effect.Effect<boolean>

Returns `true` if this `Deferred` has already been completed with a value or an error, `false` otherwise.

isDone
(
const deferred: Deferred.Deferred<number, string>
deferred
)
11
12
return [
const done1: Option<Effect.Effect<number, string, never>>
done1
,
const done2: boolean
done2
]
13
})
14
15
import Effect
Effect
.
const runPromise: <(boolean | Option<Effect.Effect<number, string, never>>)[], never>(effect: Effect.Effect<(boolean | Option<Effect.Effect<number, 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<(boolean | Option<Effect.Effect<number, string, never>>)[], never, never>
program
).
(method) Promise<(boolean | Option<Effect<number, string, never>>)[]>.then<void, never>(onfulfilled?: ((value: (boolean | Option<Effect.Effect<number, string, never>>)[]) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<...>) | ... 1 more ... | 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
)
16
// Output: [ none(), false ]

In this example, we first create a Deferred and then use Deferred.poll to check its completion status. Since it’s not completed yet, done1 is none(). We also use Deferred.isDone to confirm that the Deferred is indeed not completed, so done2 is false.

Here’s a scenario where we use a Deferred to hand over a value between two fibers:

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

Creates a new `Deferred`.

make
<string, string>()
5
6
// Fiber A: Set the Deferred value after waiting for 1 second
7
const
const sendHelloWorld: Effect.Effect<boolean, never, never>
sendHelloWorld
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<void, never, never>>, boolean>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<void, never, never>>, boolean, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
8
yield*
import Effect
Effect
.
const sleep: (duration: DurationInput) => Effect.Effect<void>

Returns an effect that suspends for the specified duration. This method is asynchronous, and does not actually block the fiber executing the effect.

sleep
("1 second")
9
return yield*
import Deferred
Deferred
.
const succeed: <string, string>(self: Deferred.Deferred<string, string>, value: string) => Effect.Effect<boolean> (+1 overload)

Completes the `Deferred` with the specified value.

succeed
(
const deferred: Deferred.Deferred<string, string>
deferred
, "hello world")
10
})
11
12
// Fiber B: Wait for the Deferred and print the value
13
const
const getAndPrint: Effect.Effect<string, string, never>
getAndPrint
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<string, string, never>>, string>(f: (resume: Effect.Adapter) => Generator<YieldWrap<Effect.Effect<string, string, never>>, string, never>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
14
const
const s: string
s
= yield*
import Deferred
Deferred
.
(alias) await<string, string>(self: Deferred.Deferred<string, string>): Effect.Effect<string, string, never> export await

Retrieves the value of the `Deferred`, suspending the fiber running the workflow until the result is available.

await
(
const deferred: Deferred.Deferred<string, string>
deferred
)
15
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 s: string
s
)
16
return
const s: string
s
17
})
18
19
// Run both fibers concurrently
20
const
const fiberA: Fiber.RuntimeFiber<boolean, never>
fiberA
= yield*
import Effect
Effect
.
const fork: <boolean, never, never>(self: Effect.Effect<boolean, never, never>) => Effect.Effect<Fiber.RuntimeFiber<boolean, 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 sendHelloWorld: Effect.Effect<boolean, never, never>
sendHelloWorld
)
21
const
const fiberB: Fiber.RuntimeFiber<string, string>
fiberB
= yield*
import Effect
Effect
.
const fork: <string, string, never>(self: Effect.Effect<string, string, never>) => Effect.Effect<Fiber.RuntimeFiber<string, 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
(
const getAndPrint: Effect.Effect<string, string, never>
getAndPrint
)
22
23
// Wait for both fibers to complete
24
return yield*
import Fiber
Fiber
.
const join: <[boolean, string], string>(self: Fiber.Fiber<[boolean, string], string>) => Effect.Effect<[boolean, 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
(
import Fiber
Fiber
.
const zip: <boolean, never, string, string>(self: Fiber.Fiber<boolean, never>, that: Fiber.Fiber<string, string>) => Fiber.Fiber<[boolean, string], string> (+1 overload)

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

zip
(
const fiberA: Fiber.RuntimeFiber<boolean, never>
fiberA
,
const fiberB: Fiber.RuntimeFiber<string, string>
fiberB
))
25
})
26
27
import Effect
Effect
.
const runPromise: <[boolean, string], string>(effect: Effect.Effect<[boolean, string], string, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<[boolean, 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<[boolean, string], string, never>
program
).
(method) Promise<[boolean, string]>.then<void, void>(onfulfilled?: ((value: [boolean, string]) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => void | PromiseLike<void>) | 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
,
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.error(message?: any, ...optionalParams: any[]): void

Prints to `stderr` 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 code = 5; console.error('error #%d', code); // Prints: error #5, to stderr console.error('error', code); // Prints: error 5, to stderr ``` If formatting elements (e.g. `%d`) are not found in the first string then [`util.inspect()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilinspectobject-options) is called on each argument and the resulting string values are concatenated. See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

error
)
28
/*
29
Output:
30
hello world
31
[ true, "hello world" ]
32
*/

In this example, we have two fibers, fiberA and fiberB, that communicate using a Deferred:

  • fiberA sets the Deferred value to “hello world” after waiting for 1 second.
  • fiberB waits for the Deferred to be completed and then prints the received value to the console.

By running both fibers concurrently and using the Deferred as a synchronization point, we can ensure that fiberB only proceeds after fiberA has completed its task. This coordination mechanism allows you to hand over values or coordinate work between different parts of your program effectively.