<A, E = Cause.UnknownError>(
options:
| {
readonly try: (signal: AbortSignal) => PromiseLike<A>
readonly catch: (error: unknown) => E
}
| ((signal: AbortSignal) => PromiseLike<A>)
): Effect<A, E>Creates an Effect from an asynchronous computation that may throw or
reject, mapping failures into the error channel.
When to use
Use when you need to perform asynchronous operations that might fail, such as fetching data from an API, and want thrown exceptions or rejected promises captured as Effect errors.
Details
The promise thunk is evaluated when the effect runs. If it returns a promise that resolves, the resolved value becomes the success value. If the thunk throws before returning a promise, or if the returned promise rejects, the thrown or rejected value is mapped into the error channel.
Passing the thunk directly maps failures to Cause.UnknownError.
Passing { try, catch } uses catch to map failures to an error of type
E.
The thunk receives an AbortSignal that is aborted if the effect is
interrupted. The underlying asynchronous operation only stops if it observes
that signal.
Gotchas
If catch throws while mapping the error, that thrown value is treated as a
defect. Return the error value you want in the error channel instead of
throwing it.
Example (Wrapping a fetch request that may fail)
import { Effect } from "effect"
const getTodo = (id: number) =>
// Will catch any errors and propagate them as UnknownError
Effect.tryPromise(() =>
fetch(`https://jsonplaceholder.typicode.com/todos/${id}`)
)
// ┌─── Effect<Response, UnknownError, never>
// ▼
const program = getTodo(1)Example (Mapping Promise rejections to a tagged error)
import { Data, Effect } from "effect"
class TodoFetchError extends Data.TaggedError("TodoFetchError")<{ readonly cause: unknown }> {}
const getTodo = (id: number) =>
Effect.tryPromise({
try: () => fetch(`https://jsonplaceholder.typicode.com/todos/${id}`),
// remap the error
catch: (cause) => new TodoFetchError({ cause })
})
// ┌─── Effect<Response, TodoFetchError, never>
// ▼
const program = getTodo(1)export const const tryPromise: <
A,
E = Cause.UnknownError
>(
options:
| {
readonly try: (
signal: AbortSignal
) => PromiseLike<A>
readonly catch: (error: unknown) => E
}
| ((signal: AbortSignal) => PromiseLike<A>)
) => Effect<A, E>
Creates an Effect from an asynchronous computation that may throw or
reject, mapping failures into the error channel.
When to use
Use when you need to perform asynchronous operations that might fail, such
as fetching data from an API, and want thrown exceptions or rejected promises
captured as Effect errors.
Details
The promise thunk is evaluated when the effect runs. If it returns a promise
that resolves, the resolved value becomes the success value. If the thunk
throws before returning a promise, or if the returned promise rejects, the
thrown or rejected value is mapped into the error channel.
Passing the thunk directly maps failures to
Cause.UnknownError
.
Passing { try, catch } uses catch to map failures to an error of type
E.
The thunk receives an AbortSignal that is aborted if the effect is
interrupted. The underlying asynchronous operation only stops if it observes
that signal.
Gotchas
If catch throws while mapping the error, that thrown value is treated as a
defect. Return the error value you want in the error channel instead of
throwing it.
Example (Wrapping a fetch request that may fail)
import { Effect } from "effect"
const getTodo = (id: number) =>
// Will catch any errors and propagate them as UnknownError
Effect.tryPromise(() =>
fetch(`https://jsonplaceholder.typicode.com/todos/${id}`)
)
// ┌─── Effect<Response, UnknownError, never>
// ▼
const program = getTodo(1)
Example (Mapping Promise rejections to a tagged error)
import { Data, Effect } from "effect"
class TodoFetchError extends Data.TaggedError("TodoFetchError")<{ readonly cause: unknown }> {}
const getTodo = (id: number) =>
Effect.tryPromise({
try: () => fetch(`https://jsonplaceholder.typicode.com/todos/${id}`),
// remap the error
catch: (cause) => new TodoFetchError({ cause })
})
// ┌─── Effect<Response, TodoFetchError, never>
// ▼
const program = getTodo(1)
tryPromise: <function (type parameter) A in <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
A, function (type parameter) E in <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
E = import CauseCause.type Cause.UnknownError = /*unresolved*/ anyUnknownError>(
options: | {
readonly try: (
signal: AbortSignal
) => PromiseLike<A>
readonly catch: (error: unknown) => E
}
| ((signal: AbortSignal) => PromiseLike<A>)
options:
| { readonly try: (signal: AbortSignal) => PromiseLike<A>try: (signal: AbortSignalsignal: AbortSignal) => interface PromiseLike<T>PromiseLike<function (type parameter) A in <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
A>; readonly catch: (error: unknown) => Ecatch: (error: unknownerror: unknown) => function (type parameter) E in <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
E }
| ((signal: AbortSignalsignal: AbortSignal) => interface PromiseLike<T>PromiseLike<function (type parameter) A in <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
A>)
) => 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 <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
A, function (type parameter) E in <A, E = Cause.UnknownError>(options: {
readonly try: (signal: AbortSignal) => PromiseLike<A>;
readonly catch: (error: unknown) => E;
} | ((signal: AbortSignal) => PromiseLike<A>)): Effect<A, E>
E> = import internalinternal.const tryPromise: <
A,
E = Cause.UnknownError
>(
options:
| {
readonly try: (
signal: AbortSignal
) => PromiseLike<A>
readonly catch: (error: unknown) => E
}
| ((signal: AbortSignal) => PromiseLike<A>)
) => Effect.Effect<A, E>
tryPromise