<A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>Creates a stream from an effect.
Example (Creating a stream from an effect)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.fromEffect(Effect.succeed(42))
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 42 ]export const const fromEffect: <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Stream<A, E, R>
Creates a stream from an effect.
Example (Creating a stream from an effect)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.fromEffect(Effect.succeed(42))
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 42 ]
fromEffect = <function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>R>(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>R>): interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Stream<A, E, R>R> =>
const fromChannel: <
Arr extends Arr.NonEmptyReadonlyArray<any>,
E,
R
>(
channel: Channel.Channel<
Arr,
E,
void,
unknown,
unknown,
unknown,
R
>
) => Stream<
Arr extends Arr.NonEmptyReadonlyArray<infer A>
? A
: never,
E,
R
>
Creates a stream from a array-emitting Channel.
Example (Creating a stream from an array-emitting channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const channel = Channel.succeed([1, 2, 3] as const)
const stream = Stream.fromChannel(channel)
const result = yield* Stream.runCollect(stream)
yield* Console.log(result)
})
// Output: [ 1, 2, 3 ]
fromChannel(import ChannelChannel.const fromEffect: <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"
})
)
fromEffect(import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect, import ArrArr.const of: <A>(a: A) => NonEmptyArray<A>Wraps a single value in a NonEmptyArray.
Example (Creating a single-element array)
import { Array } from "effect"
console.log(Array.of(1)) // [1]
of)))