<E>(evaluate: LazyArg<Cause.Cause<E>>): Channel<never, E, never>Constructs a channel that fails immediately with the specified lazily
evaluated Cause.
Example (Failing with lazy causes)
import { Cause, Channel } from "effect"
// Create a channel that fails with a lazily computed cause
let attempts = 0
const failedChannel = Channel.failCauseSync(() => {
attempts += 1
return Cause.fail(`Runtime error after attempt ${attempts}`)
})
// Create a channel with die cause computation
const dieCauseChannel = Channel.failCauseSync(() => {
const operation = "load-profile"
return Cause.die(`Unexpected defect during ${operation}`)
})export const const failCauseSync: <E>(
evaluate: LazyArg<Cause.Cause<E>>
) => Channel<never, E, never>
Constructs a channel that fails immediately with the specified lazily
evaluated Cause.
Example (Failing with lazy causes)
import { Cause, Channel } from "effect"
// Create a channel that fails with a lazily computed cause
let attempts = 0
const failedChannel = Channel.failCauseSync(() => {
attempts += 1
return Cause.fail(`Runtime error after attempt ${attempts}`)
})
// Create a channel with die cause computation
const dieCauseChannel = Channel.failCauseSync(() => {
const operation = "load-profile"
return Cause.die(`Unexpected defect during ${operation}`)
})
failCauseSync = <function (type parameter) E in <E>(evaluate: LazyArg<Cause.Cause<E>>): Channel<never, E, never>E>(
evaluate: LazyArg<Cause.Cause<E>>evaluate: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<import CauseCause.type Cause.Cause = /*unresolved*/ anyCause<function (type parameter) E in <E>(evaluate: LazyArg<Cause.Cause<E>>): Channel<never, E, never>E>>
): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<never, function (type parameter) E in <E>(evaluate: LazyArg<Cause.Cause<E>>): Channel<never, E, never>E, never> => const fromPull: <
OutElem,
OutErr,
OutDone,
EX,
EnvX,
Env
>(
effect: Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
unknown,
unknown,
unknown,
Env | EnvX
>
Creates a Channel from an Effect that produces a Pull.
Example (Creating channels from pulls)
import { Channel, Effect } from "effect"
const channel = Channel.fromPull(
Effect.succeed(Effect.succeed(42))
)
fromPull(import EffectEffect.const failCauseSync: <E>(
evaluate: LazyArg<Cause.Cause<E>>
) => Effect<never, E>
Creates an Effect that represents a failure with a Cause computed lazily.
When to use
Use to defer computing a full Cause until the effect is run.
Details
The cause-producing function is evaluated each time the effect is executed.
Example (Lazily creating a Cause)
import { Cause, Effect } from "effect"
const program = Effect.failCauseSync(() =>
Cause.fail("Error computed at runtime")
)
Effect.runPromiseExit(program).then(console.log)
// Output: { _id: 'Exit', _tag: 'Failure', cause: ... }
failCauseSync(evaluate: LazyArg<Cause.Cause<E>>evaluate))