<A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>
<A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>Completes the Deferred with the specified Exit value, which will be
propagated to all fibers waiting on the value of the Deferred.
When to use
Use to complete a Deferred from an already computed Exit.
Details
The returned effect succeeds with true when this call completed the
Deferred, or false if it was already completed.
Example (Completing a Deferred with an Exit)
import { Deferred, Effect, Exit } from "effect"
const program = Effect.gen(function*() {
const deferred = yield* Deferred.make<number>()
yield* Deferred.done(deferred, Exit.succeed(42))
const value = yield* Deferred.await(deferred)
console.log(value) // 42
})export const const done: {
<A, E>(exit: Exit.Exit<A, E>): (
self: Deferred<A, E>
) => Effect<boolean>
<A, E>(
self: Deferred<A, E>,
exit: Exit.Exit<A, E>
): Effect<boolean>
}
Completes the Deferred with the specified Exit value, which will be
propagated to all fibers waiting on the value of the Deferred.
When to use
Use to complete a Deferred from an already computed Exit.
Details
The returned effect succeeds with true when this call completed the
Deferred, or false if it was already completed.
Example (Completing a Deferred with an Exit)
import { Deferred, Effect, Exit } from "effect"
const program = Effect.gen(function*() {
const deferred = yield* Deferred.make<number>()
yield* Deferred.done(deferred, Exit.succeed(42))
const value = yield* Deferred.await(deferred)
console.log(value) // 42
})
done: {
<function (type parameter) A in <A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>A, function (type parameter) E in <A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>E>(exit: Exit.Exit<A, E>exit: import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<function (type parameter) A in <A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>A, function (type parameter) E in <A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>E>): (self: Deferred<A, E>(parameter) self: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: interface Deferred<in out A, in out E = never>A Deferred represents an asynchronous variable that can be set exactly
once, with the ability for an arbitrary number of fibers to suspend (by
calling Deferred.await) and automatically resume when the variable is set.
When to use
Use to coordinate multiple fibers around a value or failure that will be
supplied exactly once.
Example (Creating a Deferred for inter-fiber communication)
import { Deferred, Effect, Fiber } from "effect"
// Create and use a Deferred for inter-fiber communication
const program = Effect.gen(function*() {
// Create a Deferred that will hold a string value
const deferred: Deferred.Deferred<string> = yield* Deferred.make<string>()
// Fork a fiber that will set the deferred value
const producer = yield* Effect.forkChild(
Effect.gen(function*() {
yield* Effect.sleep("100 millis")
yield* Deferred.succeed(deferred, "Hello, World!")
})
)
// Fork a fiber that will await the deferred value
const consumer = yield* Effect.forkChild(
Effect.gen(function*() {
const value = yield* Deferred.await(deferred)
console.log("Received:", value)
return value
})
)
// Wait for both fibers to complete
yield* Fiber.join(producer)
const result = yield* Fiber.join(consumer)
return result
})
Companion namespace containing type-level metadata for Deferred.
When to use
Use to reference type-level metadata associated with Deferred.
Deferred<function (type parameter) A in <A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>A, function (type parameter) E in <A, E>(exit: Exit.Exit<A, E>): (self: Deferred<A, E>) => Effect<boolean>E>) => interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<boolean>
<function (type parameter) A in <A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>E>(self: Deferred<A, E>(parameter) self: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: interface Deferred<in out A, in out E = never>A Deferred represents an asynchronous variable that can be set exactly
once, with the ability for an arbitrary number of fibers to suspend (by
calling Deferred.await) and automatically resume when the variable is set.
When to use
Use to coordinate multiple fibers around a value or failure that will be
supplied exactly once.
Example (Creating a Deferred for inter-fiber communication)
import { Deferred, Effect, Fiber } from "effect"
// Create and use a Deferred for inter-fiber communication
const program = Effect.gen(function*() {
// Create a Deferred that will hold a string value
const deferred: Deferred.Deferred<string> = yield* Deferred.make<string>()
// Fork a fiber that will set the deferred value
const producer = yield* Effect.forkChild(
Effect.gen(function*() {
yield* Effect.sleep("100 millis")
yield* Deferred.succeed(deferred, "Hello, World!")
})
)
// Fork a fiber that will await the deferred value
const consumer = yield* Effect.forkChild(
Effect.gen(function*() {
const value = yield* Deferred.await(deferred)
console.log("Received:", value)
return value
})
)
// Wait for both fibers to complete
yield* Fiber.join(producer)
const result = yield* Fiber.join(consumer)
return result
})
Companion namespace containing type-level metadata for Deferred.
When to use
Use to reference type-level metadata associated with Deferred.
Deferred<function (type parameter) A in <A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>E>, exit: Exit.Exit<A, E>exit: import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<function (type parameter) A in <A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Deferred<A, E>, exit: Exit.Exit<A, E>): Effect<boolean>E>): interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<boolean>
} = const completeWith: {
<A, E>(effect: Effect<A, E>): (
self: Deferred<A, E>
) => Effect<boolean>
<A, E>(
self: Deferred<A, E>,
effect: Effect<A, E>
): Effect<boolean>
}
Attempts to complete the Deferred with the specified effect directly.
When to use
Use to store an already environment-free effect as the completion without
running it during completion.
Details
The returned effect succeeds with true when this call completed the
Deferred, or false if it was already completed.
Gotchas
The supplied effect is not memoized by completeWith; each awaiter may run
the stored effect independently.
Example (Completing a Deferred with an effect)
import { Deferred, Effect } from "effect"
const program = Effect.gen(function*() {
const deferred = yield* Deferred.make<number>()
const completed = yield* Deferred.completeWith(deferred, Effect.succeed(42))
console.log(completed) // true
const value = yield* Deferred.await(deferred)
console.log(value) // 42
})
completeWith as any