Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to Use
Effect.gen allows you to write code that looks and behaves like synchronous
code, but it can handle asynchronous tasks, errors, and complex control flow
(like loops and conditions). It helps make asynchronous code more readable
and easier to manage.
The generator functions work similarly to async/await but with more
explicit control over the execution of effects. You can yield* values from
effects and return the final result at the end.
Transforms or modifies the error produced by an effect without affecting its
success value.
When to Use
This function is helpful when you want to enhance the error with additional
information, change the error type, or apply custom error handling while
keeping the original behavior of the effect's success values intact. It only
operates on the error channel and leaves the success channel unchanged.
Represents the completion of an asynchronous operation
Promise<unknown>) =>void
6
>() {}
7
8
const
consteffectWaitUntil: <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal) =>Effect.Effect<void, never, WaitUntil|R>
effectWaitUntil= <
function (typeparameter) Ain <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal):Effect.Effect<void, never, WaitUntil|R>
A,
function (typeparameter) Ein <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal):Effect.Effect<void, never, WaitUntil|R>
E,
function (typeparameter) Rin <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal):Effect.Effect<void, never, WaitUntil|R>
R>(
9
effect: Effect.Effect<A, E, R>
effect:
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
interfaceEffect<outA, outE=never, outR=never>
The Effect interface defines a value that describes a workflow or job,
which can succeed or fail.
Details
The Effect interface represents a computation that can model a workflow
involving various types of operations, such as synchronous, asynchronous,
concurrent, and parallel interactions. It operates within a context of type
R, and the result can either be a success with a value of type A or a
failure with an error of type E. The Effect is designed to handle complex
interactions with external resources, offering advanced features such as
fiber-based concurrency, scheduling, interruption handling, and scalability.
This makes it suitable for tasks that require fine-grained control over
concurrency and error management.
To execute an Effect value, you need a Runtime, which provides the
environment necessary to run and manage the computation.
@since ― 2.0.0
@since ― 2.0.0
Effect<
function (typeparameter) Ain <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal):Effect.Effect<void, never, WaitUntil|R>
A,
function (typeparameter) Ein <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal):Effect.Effect<void, never, WaitUntil|R>
E,
function (typeparameter) Rin <A, E, R>(effect:Effect.Effect<A, E, R>, abortSignal?:AbortSignal):Effect.Effect<void, never, WaitUntil|R>
R>,
10
abortSignal: AbortSignal |undefined
abortSignal?:
interfaceAbortSignal
A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object.
} |undefined) => <A, E, R>(self:Effect.Effect<...>) =>Effect.Effect<...> (+1overload)
Combines two effects into a single effect, producing a tuple of their
results.
Details
This function combines two effects, self and that, into one. It executes
the first effect (self) and then the second effect (that), collecting
their results into a tuple. Both effects must succeed for the resulting
effect to succeed. If either effect fails, the entire operation fails.
By default, the effects are executed sequentially. If the concurrent option
is set to true, the effects will run concurrently, potentially improving
performance for independent operations.
flatMap lets you sequence effects so that the result of one effect can be
used in the next step. It is similar to flatMap used with arrays but works
specifically with Effect instances, allowing you to avoid deeply nested
effect structures.
Since effects are immutable, flatMap always returns a new effect instead of
changing the original one.
When to Use
Use flatMap when you need to chain multiple effects, ensuring that each
step produces a new Effect while flattening any nested effects that may
occur.
Example
import { pipe, Effect } from"effect"
// Function to apply a discount safely to a transaction amount
constapplyDiscount= (
total:number,
discountRate:number
):Effect.Effect<number, Error> =>
discountRate ===0
? Effect.fail(newError("Discount rate cannot be zero"))
Creates an Effect that represents a synchronous side-effectful computation.
Details
The provided function (thunk) must not throw errors; if it does, the error
will be treated as a "defect".
This defect is not a standard error but indicates a flaw in the logic that
was expected to be error-free. You can think of it similar to an unexpected
crash in the program, which can be further managed or logged using tools like
catchAllDefect
.
When to Use
Use this function when you are sure the operation will not fail.
Example (Logging a Message)
import { Effect } from"effect"
constlog= (message:string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
constprogram=log("Hello, World!")
@see ― try_try for a version that can handle failures.
@since ― 2.0.0
sync(() =>
16
waitUntil: (promise:Promise<unknown>) =>void
waitUntil(
import Runtime
Runtime.
construnPromise: <R, A, E>(runtime:Runtime.Runtime<R>, effect:Effect.Effect<A, E, R>, options?: {
readonlysignal?:AbortSignal;
} |undefined) =>Promise<...> (+1overload)
Runs the Effect, returning a JavaScript Promise that will be resolved
with the value of the effect once the effect has been executed, or will be
rejected with the first error or exception throw by the effect.
This method is effectful and should only be used at the edges of your
program.