<S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<
Success<S[number]>,
Error<S[number]>,
Services<S[number]>
>Runs all streams concurrently until one stream emits its first value, then mirrors that winning stream and interrupts the rest.
Details
Failures or completion from losing streams before a winner is chosen are ignored unless every stream fails or completes before emitting. After a winner is chosen, that stream's later failures are propagated.
Example (Racing multiple streams)
import { Console, Effect, Schedule, Stream } from "effect"
const program = Effect.gen(function*() {
const result = yield* Stream.raceAll(
Stream.fromSchedule(Schedule.spaced("1 second")),
Stream.make(0, 1, 2)
).pipe(Stream.runCollect)
yield* Console.log(result)
})
Effect.runPromise(program)
// Output: [ 0, 1, 2 ]export const const raceAll: <
S extends ReadonlyArray<Stream<any, any, any>>
>(
...streams: S
) => Stream<
Success<S[number]>,
Error<S[number]>,
Services<S[number]>
>
Runs all streams concurrently until one stream emits its first value, then
mirrors that winning stream and interrupts the rest.
Details
Failures or completion from losing streams before a winner is chosen are
ignored unless every stream fails or completes before emitting. After a
winner is chosen, that stream's later failures are propagated.
Example (Racing multiple streams)
import { Console, Effect, Schedule, Stream } from "effect"
const program = Effect.gen(function*() {
const result = yield* Stream.raceAll(
Stream.fromSchedule(Schedule.spaced("1 second")),
Stream.make(0, 1, 2)
).pipe(Stream.runCollect)
yield* Console.log(result)
})
Effect.runPromise(program)
// Output: [ 0, 1, 2 ]
raceAll = <function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S extends interface ReadonlyArray<T>ReadonlyArray<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<any, any, any>>>(
...streams: S extends ReadonlyArray<Stream<any, any, any>>streams: function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S
): 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<type Success<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _A
: never
Extract the success type from a Stream type.
Example (Extracting the success type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type SuccessType = Stream.Success<NumberStream>
// SuccessType is number
Success<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>, type Error<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _E
: never
Extract the error type from a Stream type.
Example (Extracting the error type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type ErrorType = Stream.Error<NumberStream>
// ErrorType is string
Error<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>, type Services<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _R
: never
Extract the services type from a Stream type.
Example (Extracting the services type from a Stream type)
import type { Stream } from "effect"
interface Database {
query: (sql: string) => unknown
}
type NumberStream = Stream.Stream<number, string, { db: Database }>
type RequiredServices = Stream.Services<NumberStream>
// RequiredServices is { db: Database }
Services<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>> =>
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.fromTransform((_: Pull.Pull<
unknown,
unknown,
unknown,
never
>
(parameter) _: {
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;
}
_, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope) =>
import EffectEffect.sync(() => {
let let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner:
| import PullPull.type Pull.Pull = /*unresolved*/ anyPull<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<type Success<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _A
: never
Extract the success type from a Stream type.
Example (Extracting the success type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type SuccessType = Stream.Success<NumberStream>
// SuccessType is number
Success<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>>, type Error<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _E
: never
Extract the error type from a Stream type.
Example (Extracting the error type from a Stream type)
import type { Stream } from "effect"
type NumberStream = Stream.Stream<number, string, never>
type ErrorType = Stream.Error<NumberStream>
// ErrorType is string
Error<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>, void, type Services<
T extends Stream<any, any, any>
> = [T] extends [
Stream<infer _A, infer _E, infer _R>
]
? _R
: never
Extract the services type from a Stream type.
Example (Extracting the services type from a Stream type)
import type { Stream } from "effect"
interface Database {
query: (sql: string) => unknown
}
type NumberStream = Stream.Stream<number, string, { db: Database }>
type RequiredServices = Stream.Services<NumberStream>
// RequiredServices is { db: Database }
Services<function (type parameter) S in <S extends ReadonlyArray<Stream<any, any, any>>>(...streams: S): Stream<Success<S[number]>, Error<S[number]>, Services<S[number]>>S[number]>>
| undefined
const const race: Effect.Effect<
readonly [any, ...any[]],
any,
any
>
const race: {
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;
}
race = import EffectEffect.raceAll(streams: S extends ReadonlyArray<Stream<any, any, any>>streams.ReadonlyArray<Stream<any, any, any>>.map<any>(callbackfn: (value: Stream<any, any, any>, index: number, array: readonly Stream<any, any, any>[]) => any, thisArg?: any): any[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((stream: Stream<any, any, any>(parameter) stream: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
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; <…;
}
stream) => {
const const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope = import ScopeScope.forkUnsafe(scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope)
return import ChannelChannel.toPullScoped(stream: Stream<any, any, any>(parameter) stream: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
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; <…;
}
stream.Stream<out A, out E = never, out R = never>.channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>(property) Stream<out A, out E = never, out R = never>.channel: {
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; <…;
}
channel, const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope).pipe(
import EffectEffect.flatMap((pull: Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>
(parameter) pull: {
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;
}
pull) => import EffectEffect.zip(import EffectEffect.succeed(pull: Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>
(parameter) pull: {
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;
}
pull), pull: Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>
(parameter) pull: {
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;
}
pull)),
import EffectEffect.onExit((exit: Exit.Exit<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
exit) => {
if (exit: Exit.Exit<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
exit._tag === "Success") {
if (let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner) {
return import ScopeScope.close(const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope, exit: Exit.Success<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
(parameter) exit: {
_tag: "Success";
value: A;
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;
}
exit)
}
let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner = exit: Exit.Success<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
(parameter) exit: {
_tag: "Success";
value: A;
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;
}
exit.value[0]
return import EffectEffect.void
}
return import ScopeScope.close(const childScope: Scope.Closeableconst childScope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
childScope, exit: Exit.Failure<
[
Pull.Pull<
readonly [any, ...any[]],
any,
void,
any
>,
readonly [any, ...any[]]
],
any
>
(parameter) exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
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;
}
exit)
}),
import EffectEffect.map(([, chunk: any(parameter) chunk: {
0: any;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<any>>): Array<any>; (...items: Array<any>): Array<any> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<any>;
indexOf: (searchElement: any, fromIndex?: number) => number;
lastIndexOf: (searchElement: any, fromIndex?: number) => number;
every: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => void, thisArg?: any) => void;
map: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): Array<S>; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): Array<any> };
reduce: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
reduceRight: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
find: { (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findIndex: (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, any]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<any>;
includes: (searchElement: any, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: any, index: number, array: Array<any>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => any;
findLast: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findLastIndex: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
toReversed: () => Array<any>;
toSorted: (compareFn?: ((a: any, b: any) => number) | undefined) => Array<any>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<any>): Array<any>; (start: number, deleteCount?: number): Array<any> };
with: (index: number, value: any) => Array<any>;
}
chunk]) => chunk: any(parameter) chunk: {
0: any;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<any>>): Array<any>; (...items: Array<any>): Array<any> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<any>;
indexOf: (searchElement: any, fromIndex?: number) => number;
lastIndexOf: (searchElement: any, fromIndex?: number) => number;
every: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => void, thisArg?: any) => void;
map: (callbackfn: (value: any, index: number, array: ReadonlyArray<any>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): Array<S>; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): Array<any> };
reduce: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
reduceRight: { (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any): any; (callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: ReadonlyArray<any>) => any, initialValu…;
find: { (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findIndex: (predicate: (value: any, index: number, obj: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, any]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<any>;
includes: (searchElement: any, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: any, index: number, array: Array<any>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => any;
findLast: { (predicate: (value: any, index: number, array: ReadonlyArray<any>) => value is S, thisArg?: any): S | undefined; (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any): any };
findLastIndex: (predicate: (value: any, index: number, array: ReadonlyArray<any>) => unknown, thisArg?: any) => number;
toReversed: () => Array<any>;
toSorted: (compareFn?: ((a: any, b: any) => number) | undefined) => Array<any>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<any>): Array<any>; (start: number, deleteCount?: number): Array<any> };
with: (index: number, value: any) => Array<any>;
}
chunk)
)
}))
return import EffectEffect.suspend(() => let winner:
| Pull.Pull<
Arr.NonEmptyReadonlyArray<
Success<S[number]>
>,
Error<S[number]>,
void,
Services<S[number]>
>
| undefined
winner ?? const race: Effect.Effect<
readonly [any, ...any[]],
any,
any
>
const race: {
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;
}
race)
})
))