Documentation
Streaming
Sink
Introduction to Sinks

Introduction to Sinks

In the world of streams, a Sink<R, E, In, L, Z> plays a crucial role. It's like a specialized function designed to consume elements generated by a Stream. Here's a breakdown of what a Sink does:

  • It can consume a varying number of In elements, which might be zero, one, or more.
  • It has the potential to encounter errors of type E.
  • Ultimately, it produces a value of type Z.
  • Additionally, it can return a remainder of type L, which represents any leftover elements.

To use a Sink for processing a stream, you simply pass it to the Stream.run function:

import { Stream, Sink, Effect } from "effect"
 
// $ExpectType Stream<never, never, number>
const stream = Stream.make(1, 2, 3) // Define a stream with numbers 1, 2, and 3
 
// $ExpectType Sink<never, never, number, never, number>
const sink = Sink.sum // Choose a sink that sums up numbers
 
// $ExpectType Effect<never, never, number>
const sum = Stream.run(stream, sink) // Run the stream through the sink
 
Effect.runPromise(sum).then(console.log)
/*
Output:
6
*/