Hyperlinkv0.8.0-beta.28

Channel

Channel.fromEffectconsteffect/Channel.ts:1114
<A, E, R>(effect: Effect.Effect<A, E, R>): Channel<
  A,
  Pull.ExcludeDone<E>,
  void,
  unknown,
  unknown,
  unknown,
  R
>

Uses an effect to write a single value to the channel.

Example (Creating channels from effects)

import { Channel, Data, Effect } from "effect"

class DatabaseError extends Data.TaggedError("DatabaseError")<{
  readonly message: string
}> {}

// Create a channel from a successful effect
const successChannel = Channel.fromEffect(
  Effect.succeed("Hello from effect!")
)

// Create a channel from an effect that might fail
const fetchUserChannel = Channel.fromEffect(
  Effect.tryPromise({
    try: () => fetch("/api/user").then((res) => res.json()),
    catch: (error) => new DatabaseError({ message: String(error) })
  })
)

// Channel from effect with async computation
const asyncChannel = Channel.fromEffect(
  Effect.gen(function*() {
    yield* Effect.sleep("100 millis")
    return "Async result"
  })
)
constructors
Source effect/Channel.ts:111413 lines
export const fromEffect = <A, E, R>(
  effect: Effect.Effect<A, E, R>
): Channel<A, Pull.ExcludeDone<E>, void, unknown, unknown, unknown, R> =>
  fromPull(
    Effect.sync(() => {
      let done = false
      return Effect.suspend((): Pull.Pull<A, E, void, R> => {
        if (done) return Cause.done()
        done = true
        return effect
      })
    })
  )
Referenced by 3 symbols