Hyperlinkv0.8.0-beta.28

Stream

Stream.fromIterableEffectconsteffect/Stream.ts:1152
<A, E, R>(iterable: Effect.Effect<Iterable<A>, E, R>): Stream<A, E, R>

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" ]
constructors
Source effect/Stream.ts:11522 lines
export const fromIterableEffect = <A, E, R>(iterable: Effect.Effect<Iterable<A>, E, R>): Stream<A, E, R> =>
  unwrap(Effect.map(iterable, fromIterable))