<E>(evaluate: LazyArg<E>): Stream<never, E>Terminates with the specified lazily evaluated error.
Example (Failing a stream lazily)
import { Console, Effect, Stream } from "effect"
const stream = Stream.failSync(() => "Uh oh!")
const program = Effect.gen(function*() {
const exit = yield* Stream.runCollect(stream).pipe(Effect.exit)
yield* Console.log(exit)
})
Effect.runPromise(program)
// Output:
// { _id: 'Exit', _tag: 'Failure', cause: { _id: 'Cause', _tag: 'Fail', failure: 'Uh oh!' } }export const const failSync: <E>(
evaluate: LazyArg<E>
) => Stream<never, E>
Terminates with the specified lazily evaluated error.
Example (Failing a stream lazily)
import { Console, Effect, Stream } from "effect"
const stream = Stream.failSync(() => "Uh oh!")
const program = Effect.gen(function*() {
const exit = yield* Stream.runCollect(stream).pipe(Effect.exit)
yield* Console.log(exit)
})
Effect.runPromise(program)
// Output:
// { _id: 'Exit', _tag: 'Failure', cause: { _id: 'Cause', _tag: 'Fail', failure: 'Uh oh!' } }
failSync = <function (type parameter) E in <E>(evaluate: LazyArg<E>): Stream<never, E>E>(evaluate: LazyArg<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<function (type parameter) E in <E>(evaluate: LazyArg<E>): Stream<never, E>E>): interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<never, function (type parameter) E in <E>(evaluate: LazyArg<E>): Stream<never, E>E> => const fromChannel: <
Arr extends Arr.NonEmptyReadonlyArray<any>,
E,
R
>(
channel: Channel.Channel<
Arr,
E,
void,
unknown,
unknown,
unknown,
R
>
) => Stream<
Arr extends Arr.NonEmptyReadonlyArray<infer A>
? A
: never,
E,
R
>
Creates a stream from a array-emitting Channel.
Example (Creating a stream from an array-emitting channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const channel = Channel.succeed([1, 2, 3] as const)
const stream = Stream.fromChannel(channel)
const result = yield* Stream.runCollect(stream)
yield* Console.log(result)
})
// Output: [ 1, 2, 3 ]
fromChannel(import ChannelChannel.const failSync: <E>(
evaluate: LazyArg<E>
) => Channel<never, E, never>
Constructs a channel that fails immediately with the specified lazily
evaluated error.
When to use
Use when the error value should be computed each time the channel runs instead
of when the channel is constructed.
Example (Failing with a lazy error)
import { Channel } from "effect"
// Create a channel that fails with a lazily computed error
const failedChannel = Channel.failSync(() => {
console.log("Computing error...")
return new Error("Computed at runtime")
})
// The error computation is deferred until the channel runs
let attempts = 0
const conditionalError = Channel.failSync(() => {
attempts += 1
return `Error after attempt ${attempts}`
})
// Use with expensive error construction
const expensiveError = Channel.failSync(() => {
const requestId = "request-123"
return new Error(`Failed while processing ${requestId}`)
})
failSync(evaluate: LazyArg<E>evaluate))