<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>Returns the current completion effect as an Option. This returns
Option.some(effect) when the Deferred is completed, Option.none()
otherwise.
When to use
Use to inspect whether a Deferred is already completed and retrieve its
stored completion effect when available.
Example (Polling Deferred completion)
import { Deferred, Effect } from "effect"
const program = Effect.gen(function*() {
const deferred = yield* Deferred.make<number>()
const beforeCompletion = yield* Deferred.poll(deferred)
console.log(beforeCompletion._tag === "None") // true
yield* Deferred.succeed(deferred, 42)
const afterCompletion = yield* Deferred.poll(deferred)
console.log(afterCompletion._tag === "Some") // true
})export function function poll<A, E>(
self: Deferred<A, E>
): Effect<Option.Option<Effect<A, E>>>
Returns the current completion effect as an Option. This returns
Option.some(effect) when the Deferred is completed, Option.none()
otherwise.
When to use
Use to inspect whether a Deferred is already completed and retrieve its
stored completion effect when available.
Example (Polling Deferred completion)
import { Deferred, Effect } from "effect"
const program = Effect.gen(function*() {
const deferred = yield* Deferred.make<number>()
const beforeCompletion = yield* Deferred.poll(deferred)
console.log(beforeCompletion._tag === "None") // true
yield* Deferred.succeed(deferred, 42)
const afterCompletion = yield* Deferred.poll(deferred)
console.log(afterCompletion._tag === "Some") // true
})
poll<function (type parameter) A in poll<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>A, function (type parameter) E in poll<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>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 poll<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>A, function (type parameter) E in poll<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>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<import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<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<function (type parameter) A in poll<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>A, function (type parameter) E in poll<A, E>(self: Deferred<A, E>): Effect<Option.Option<Effect<A, E>>>E>>> {
return import internalEffectinternalEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(() => import OptionOption.const fromUndefinedOr: <A>(
a: A
) => Option<Exclude<A, undefined>>
Converts a possibly undefined value into an Option, leaving null
as a valid Some.
When to use
Use when you want to treat only undefined as absent while preserving null
as a meaningful value.
Details
undefined → None
- Any other value (including
null) → Some
Example (Converting possibly undefined values to an Option)
import { Option } from "effect"
console.log(Option.fromUndefinedOr(undefined))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromUndefinedOr(null))
// Output: { _id: 'Option', _tag: 'Some', value: null }
console.log(Option.fromUndefinedOr(42))
// Output: { _id: 'Option', _tag: 'Some', value: 42 }
fromUndefinedOr(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.Deferred<A, E>.effect?: Effect<A, E>effect))
}