<In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<
void,
In,
never,
E,
R
>A sink that executes the provided effectful function for every item fed to it.
Example (Running effects for each item)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink that logs each item
const sink = Sink.forEach((item: number) => Console.log(`Processing: ${item}`))
// Use it with a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output:
// Processing: 1
// Processing: 2
// Processing: 3export const const forEach: <In, X, E, R>(
f: (input: In) => Effect.Effect<X, E, R>
) => Sink<void, In, never, E, R>
A sink that executes the provided effectful function for every item fed
to it.
Example (Running effects for each item)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink that logs each item
const sink = Sink.forEach((item: number) => Console.log(`Processing: ${item}`))
// Use it with a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output:
// Processing: 1
// Processing: 2
// Processing: 3
forEach = <function (type parameter) In in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>In, function (type parameter) X in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>X, function (type parameter) E in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>E, function (type parameter) R in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>R>(
f: (input: In) => Effect.Effect<X, E, R>f: (input: Ininput: function (type parameter) In in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>In) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) X in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>X, function (type parameter) E in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>E, function (type parameter) R in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>R>
): interface Sink<out A, in In = unknown, out L = never, out E = never, out R = never>A Sink<A, In, L, E, R> is used to consume elements produced by a Stream.
You can think of a sink as a function that will consume a variable amount of
In elements (could be 0, 1, or many), might fail with an error of type E,
and will eventually yield a value of type A together with a remainder of
type L (i.e. any leftovers).
Example (Running a sink with a stream)
import { Effect, Sink, Stream } from "effect"
// Create a simple sink that always succeeds with a value
const sink: Sink.Sink<number> = Sink.succeed(42)
// Use the sink to consume a stream
const stream = Stream.make(1, 2, 3)
const program = Stream.run(stream, sink)
Effect.runPromise(program).then(console.log)
// Output: 42
Namespace containing types and interfaces for Sink variance and type relationships.
Sink<void, function (type parameter) In in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>In, never, function (type parameter) E in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>E, function (type parameter) R in <In, X, E, R>(f: (input: In) => Effect.Effect<X, E, R>): Sink<void, In, never, E, R>R> => const forEachArray: <In, X, E, R>(
f: (
input: NonEmptyReadonlyArray<In>
) => Effect.Effect<X, E, R>
) => Sink<void, In, never, E, R>
A sink that executes the provided effectful function for every Chunk fed
to it.
Example (Running effects for each chunk)
import { Console, Effect, Sink, Stream } from "effect"
// Create a sink that processes chunks
const sink = Sink.forEachArray((chunk: ReadonlyArray<number>) =>
Console.log(
`Processing chunk of ${chunk.length} items: [${chunk.join(", ")}]`
)
)
// Use it with a stream
const stream = Stream.make(1, 2, 3, 4, 5)
const program = Stream.run(stream, sink)
Effect.runPromise(program)
// Output: Processing chunk of 5 items: [1, 2, 3, 4, 5]
forEachArray(import EffectEffect.forEach((_: any_) => f: (input: In) => Effect.Effect<X, E, R>f(_: any_), { discard: booleandiscard: true }))