effect-io-ai

Package: effect
Module: Stream

Stream.fromIterableEffect

Creates a stream from an effect producing an iterable of values.

When to use

Use when the iterable must be acquired from an Effect before the stream emits, and acquisition services or failures should be part of the stream.

Example (Creating a stream from an iterable effect)

import { Console, Context, Effect, Stream } from "effect"

class UserRepo extends Context.Service<UserRepo, {
  readonly list: Effect.Effect<ReadonlyArray<string>>
}>()("UserRepo") {}

const listUsers = Effect.service(UserRepo).pipe(
  Effect.andThen((repo) => repo.list)
)

const stream = Stream.fromIterableEffect(listUsers)

const program = Effect.gen(function*() {
  const users = yield* stream.pipe(
    Stream.provideService(UserRepo, {
      list: Effect.succeed(["user1", "user2"])
    }),
    Stream.runCollect
  )
  yield* Console.log(users)
})

Effect.runPromise(program)
// Output: [ "user1", "user2" ]

Signature

declare const fromIterableEffect: <A, E, R>(iterable: Effect.Effect<Iterable<A>, E, R>) => Stream<A, E, R>

Source

Since v2.0.0