Hyperlinkv0.8.0-beta.28

Stream

Stream.toAsyncIterableEffectconsteffect/Stream.ts:11402
<A, E, R>(self: Stream<A, E, R>): Effect.Effect<
  AsyncIterable<A>,
  never,
  R
>

Creates an effect that yields an AsyncIterable using the current services.

When to use

Use when the AsyncIterable should be created inside Effect with the current context supplying the stream's services.

Example (Creating an AsyncIterable effect)

import { Effect, Stream } from "effect"

const stream = Stream.make(1, 2, 3)

const program = Effect.gen(function*() {
  const iterable = yield* Stream.toAsyncIterableEffect(stream)
  const values = yield* Effect.promise(async () => {
    const collected: Array<number> = []
    for await (const value of iterable) {
      collected.push(value)
    }
    return collected
  })
  yield* Effect.sync(() => console.log(values))
})

Effect.runPromise(program)
// [ 1, 2, 3 ]
destructors
export const toAsyncIterableEffect = <A, E, R>(self: Stream<A, E, R>): Effect.Effect<AsyncIterable<A>, never, R> =>
  Effect.map(
    Effect.context<R>(),
    (context) => toAsyncIterableWith(self, context)
  )