Scheduling Streams
On this page
schedule
When working with streams, you might need to introduce specific time intervals between each emission of stream elements. This can be achieved using the Stream.schedule
combinator.
ts
import {Stream ,Schedule ,Console ,Effect } from "effect"conststream =Stream .make (1, 2, 3, 4, 5).pipe (Stream .schedule (Schedule .spaced ("1 seconds")),Stream .tap (Console .log ))Effect .runPromise (Stream .runCollect (stream )).then (console .log )/*Output:12345{_id: "Chunk",values: [ 1, 2, 3, 4, 5 ]}*/
ts
import {Stream ,Schedule ,Console ,Effect } from "effect"conststream =Stream .make (1, 2, 3, 4, 5).pipe (Stream .schedule (Schedule .spaced ("1 seconds")),Stream .tap (Console .log ))Effect .runPromise (Stream .runCollect (stream )).then (console .log )/*Output:12345{_id: "Chunk",values: [ 1, 2, 3, 4, 5 ]}*/
In this example, we've used the Schedule.spaced("1 seconds")
schedule to introduce a one-second gap between each emission in the stream.