<A>(evaluate: LazyArg<A>): Channel<A>Creates a Channel that emits a single value computed by a lazy evaluation.
Example (Computing values lazily)
import { Channel } from "effect"
let requests = 0
const channel = Channel.sync(() => {
requests += 1
return `request-${requests}`
})
// Emits "request-1" when the channel runs for the first timeexport const const sync: <A>(
evaluate: LazyArg<A>
) => Channel<A>
Creates a Channel that emits a single value computed by a lazy evaluation.
Example (Computing values lazily)
import { Channel } from "effect"
let requests = 0
const channel = Channel.sync(() => {
requests += 1
return `request-${requests}`
})
// Emits "request-1" when the channel runs for the first time
sync = <function (type parameter) A in <A>(evaluate: LazyArg<A>): Channel<A>A>(evaluate: LazyArg<A>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<function (type parameter) A in <A>(evaluate: LazyArg<A>): Channel<A>A>): 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<function (type parameter) A in <A>(evaluate: LazyArg<A>): Channel<A>A> => const fromEffect: <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Channel<
A,
Pull.ExcludeDone<E>,
void,
unknown,
unknown,
unknown,
R
>
Uses an effect to write a single value to the channel.
Example (Creating channels from effects)
import { Channel, Data, Effect } from "effect"
class DatabaseError extends Data.TaggedError("DatabaseError")<{
readonly message: string
}> {}
// Create a channel from a successful effect
const successChannel = Channel.fromEffect(
Effect.succeed("Hello from effect!")
)
// Create a channel from an effect that might fail
const fetchUserChannel = Channel.fromEffect(
Effect.tryPromise({
try: () => fetch("/api/user").then((res) => res.json()),
catch: (error) => new DatabaseError({ message: String(error) })
})
)
// Channel from effect with async computation
const asyncChannel = Channel.fromEffect(
Effect.gen(function*() {
yield* Effect.sleep("100 millis")
return "Async result"
})
)
fromEffect(import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(evaluate: LazyArg<A>evaluate))