Deferred
A Deferred<Success, Error>
is a specialized subtype of Effect
that acts like a one-time variable with some unique characteristics. It can only be completed once, making it a useful tool for managing asynchronous operations and synchronization between different parts of your program.
A deferred is essentially a synchronization primitive that represents a value that may not be available right away. When you create a deferred, it starts out empty. Later, it can be completed with either a success value Success
or an error value Error
:
┌─── Represents the success type │ ┌─── Represents the error type │ │ ▼ ▼Deferred<Success, Error>
Once completed, it cannot be changed again.
When a fiber calls Deferred.await
, it will pause until the deferred is completed. While the fiber is waiting, it doesn’t block the thread, it only blocks semantically. This means other fibers can still run, ensuring efficient concurrency.
A deferred is conceptually similar to JavaScript’s Promise
.
The key difference is that it supports both success and error types, giving more type safety.
A deferred can be created using the Deferred.make
constructor. This returns an effect that represents the creation of the deferred. Since the creation of a deferred involves memory allocation, it must be done within an effect to ensure safe management of resources.
Example (Creating a Deferred)
1import { import Deferred
Deferred } from "effect"2
3// ┌─── Effect<Deferred<string, Error>>4// ▼5const const deferred: Effect<Deferred.Deferred<string, Error>, never, never>
deferred = import Deferred
Deferred.const make: <string, Error>() => Effect<Deferred.Deferred<string, Error>, never, never>
Creates a new `Deferred`.
make<string, interface Error
Error>()
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.
1import { import Effect
Effect, import Deferred
Deferred } from "effect"2
3// ┌─── Effect<Deferred<string, Error>, never, never>4// ▼5const const deferred: Effect.Effect<Deferred.Deferred<string, Error>, never, never>
deferred = import Deferred
Deferred.const make: <string, Error>() => Effect.Effect<Deferred.Deferred<string, Error>, never, never>
Creates a new `Deferred`.
make<string, interface Error
Error>()6
7// ┌─── Effect<string, Error, never>8// ▼9const const value: Effect.Effect<string, Error, never>
value = const deferred: Effect.Effect<Deferred.Deferred<string, Error>, never, never>
deferred.(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(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(import Deferred
Deferred.(alias) const await: <A, E>(self: Deferred.Deferred<A, E>) => Effect.Effect<A, E>
export await
Retrieves the value of the `Deferred`, suspending the fiber running the
workflow until the result is available.
await))
You can complete a deferred in several ways, depending on whether you want to succeed, fail, or interrupt the waiting fibers:
API | Description |
---|---|
Deferred.succeed | Completes the deferred successfully with a value. |
Deferred.done | Completes the deferred with an Exit value. |
Deferred.complete | Completes the deferred with the result of an effect. |
Deferred.completeWith | Completes the deferred with an effect. This effect will be executed by each waiting fiber, so use it carefully. |
Deferred.fail | Fails the deferred with an error. |
Deferred.die | Defects the deferred with a user-defined error. |
Deferred.failCause | Fails or defects the deferred with a Cause. |
Deferred.interrupt | Interrupts the deferred, forcefully stopping or interrupting the waiting fibers. |
Example (Completing a Deferred with Success)
1import { import Effect
Effect, import Deferred
Deferred } from "effect"2
3const const program: Effect.Effect<void, string, never>
program = import Effect
Effect.const gen: <YieldWrap<Effect.Effect<Deferred.Deferred<number, string>, never, never>> | YieldWrap<Effect.Effect<boolean, never, never>> | YieldWrap<...>, void>(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 // Complete the Deferred successfully7 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)8
9 // Awaiting the Deferred to get its value10 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)11
12 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 value: number
value)13})14
15import Effect
Effect.const runFork: <void, string>(effect: Effect.Effect<void, string, never>, options?: RunForkOptions) => RuntimeFiber<void, string>
Executes an effect and returns a `RuntimeFiber` that represents the running computation.
Use `runFork` when you want to start an effect without blocking the current execution flow.
It returns a fiber that you can observe, interrupt, or join as needed.
runFork(const program: Effect.Effect<void, string, never>
program)16// Output: 1
Completing a deferred produces an Effect<boolean>
. This effect returns true
if the deferred was successfully completed, and false
if it had already been completed previously. This can be useful for tracking the state of the deferred.
Example (Checking Completion Status)
1import { import Effect
Effect, import Deferred
Deferred } from "effect"2
3const const program: Effect.Effect<void, never, never>
program = import Effect
Effect.const gen: <YieldWrap<Effect.Effect<Deferred.Deferred<number, string>, never, never>> | YieldWrap<Effect.Effect<boolean, never, never>>, void>(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 // Attempt to fail the Deferred7 const const firstAttempt: boolean
firstAttempt = 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!")8
9 // Attempt to succeed after it has already been completed10 const const secondAttempt: boolean
secondAttempt = 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)11
12 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 firstAttempt: boolean
firstAttempt, const secondAttempt: boolean
secondAttempt])13})14
15import Effect
Effect.const runFork: <void, never>(effect: Effect.Effect<void, never, never>, options?: RunForkOptions) => RuntimeFiber<void, never>
Executes an effect and returns a `RuntimeFiber` that represents the running computation.
Use `runFork` when you want to start an effect without blocking the current execution flow.
It returns a fiber that you can observe, interrupt, or join as needed.
runFork(const program: Effect.Effect<void, never, never>
program)16// Output: [ true, false ]
Sometimes, you might need to check if a deferred has been completed without suspending the fiber. This can be done using the Deferred.poll
method. Here’s how it works:
Deferred.poll
returns anOption<Effect<A, E>>
:- If the
Deferred
is incomplete, it returnsNone
. - If the
Deferred
is complete, it returnsSome
, which contains the result or error.
- If the
Additionally, you can use the Deferred.isDone
function to check if a deferred has been completed. This method returns an Effect<boolean>
, which evaluates to true
if the Deferred
is completed, allowing you to quickly check its state.
Example (Polling and Checking Completion Status)
1import { import Effect
Effect, import Deferred
Deferred } from "effect"2
3const const program: Effect.Effect<void, 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<...>, void>(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 to check if it's completed7 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 has been completed10 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 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 done1: Option<Effect.Effect<number, string, never>>
done1, const done2: boolean
done2])13})14
15import Effect
Effect.const runFork: <void, never>(effect: Effect.Effect<void, never, never>, options?: RunForkOptions) => RuntimeFiber<void, never>
Executes an effect and returns a `RuntimeFiber` that represents the running computation.
Use `runFork` when you want to start an effect without blocking the current execution flow.
It returns a fiber that you can observe, interrupt, or join as needed.
runFork(const program: Effect.Effect<void, never, never>
program)16/*17Output:18[ { _id: 'Option', _tag: 'None' }, false ]19*/
Deferred
becomes 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:
Use Case | Description |
---|---|
Coordinating Fibers | When you have multiple concurrent tasks 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. |
Example (Using Deferred to Coordinate Two Fibers)
In this example, a deferred is used to pass a value between two fibers.
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.
1import { import Effect
Effect, import Deferred
Deferred, import Fiber
Fiber } from "effect"2
3const const program: Effect.Effect<void, 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<...>, void>(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 // Completes the Deferred with a value after a delay7 const const taskA: Effect.Effect<boolean, never, never>
taskA = 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 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("Starting task to complete the Deferred")9 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")10 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("Completing the Deferred")11 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")12 })13
14 // Waits for the Deferred and prints the value15 const const taskB: Effect.Effect<string, string, never>
taskB = 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* () {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("Starting task to get the value from the Deferred")17 const const value: string
value = 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)18 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("Got the value from the Deferred")19 return const value: string
value20 })21
22 // Run both fibers concurrently23 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 taskA: Effect.Effect<boolean, never, never>
taskA)24 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 taskB: Effect.Effect<string, string, never>
taskB)25
26 // Wait for both fibers to complete27 const const both: [boolean, string]
both = 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))28
29 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 both: [boolean, string]
both)30})31
32import Effect
Effect.const runFork: <void, string>(effect: Effect.Effect<void, string, never>, options?: RunForkOptions) => Fiber.RuntimeFiber<void, string>
Executes an effect and returns a `RuntimeFiber` that represents the running computation.
Use `runFork` when you want to start an effect without blocking the current execution flow.
It returns a fiber that you can observe, interrupt, or join as needed.
runFork(const program: Effect.Effect<void, string, never>
program)33/*34Starting task to complete the Deferred35Starting task to get the value from the Deferred36Completing the Deferred37Got the value from the Deferred38[ true, 'hello world' ]39*/