<Svc, A, E, B>(
peers: Record<string, Svc>,
pick: (service: Svc) => Effect.Effect<A, E>,
combine: (results: ReadonlyArray<NodeResult<A, E>>) => B
): Effect.Effect<B>Gather a query field from every instance in peers (keyed by node), each call captured as a
NodeResult — a down node becomes a failed exit, never a thrown gather — then combine.
The combine sees every node's outcome, so it owns the down-node policy.
export const const combineQuery: <Svc, A, E, B>(
peers: Record<string, Svc>,
pick: (service: Svc) => Effect.Effect<A, E>,
combine: (
results: ReadonlyArray<NodeResult<A, E>>
) => B
) => Effect.Effect<B>
Gather a query field from every instance in peers (keyed by node), each call captured as a
NodeResult
— a down node becomes a failed exit, never a thrown gather — then combine.
The combine sees every node's outcome, so it owns the down-node policy.
combineQuery = <function (type parameter) Svc in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>Svc, function (type parameter) A in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>A, function (type parameter) E in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>E, function (type parameter) B in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>B>(
peers: Record<string, Svc>peers: type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<string, function (type parameter) Svc in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>Svc>,
pick: (service: Svc) => Effect.Effect<A, E>pick: (service: Svcservice: function (type parameter) Svc in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>Svc) => 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 <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>A, function (type parameter) E in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>E>,
combine: (
results: ReadonlyArray<NodeResult<A, E>>
) => B
combine: (results: ReadonlyArray<NodeResult<A, E>>results: interface ReadonlyArray<T>ReadonlyArray<interface NodeResult<A, E = never>One node's outcome for a gathered query field — node-attributed success/failure.
NodeResult<function (type parameter) A in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>A, function (type parameter) E in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>E>>) => function (type parameter) B in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>B,
): 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) B in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>B> =>
import EffectEffect.const forEach: <NodeResult<A, E>, never, never, [string, Svc][], false>(self: [string, Svc][], f: (a: [string, Svc], i: number) => Effect.Effect<NodeResult<A, E>, never, never>, options?: {
readonly concurrency?: Concurrency | undefined;
readonly discard?: false | undefined;
} | undefined) => Effect.Effect<NodeResult<A, E>[], never, never> (+1 overload)
Executes an effectful operation for each element in an Iterable.
When to use
Use to traverse an iterable with an effectful function while preserving
element order in the collected results.
Details
The forEach function applies a provided operation to each element in the
iterable, producing a new effect that returns an array of results.
If any effect fails, the iteration stops immediately (short-circuiting), and
the error is propagated.
Concurrency:
The concurrency option controls how many operations are performed
concurrently. By default, the operations are performed sequentially.
Discarding Results:
If the discard option is set to true, the intermediate results are not
collected, and the final result of the operation is void.
Example (Mapping over an iterable with effects)
import { Console, Effect } from "effect"
const result = Effect.forEach(
[1, 2, 3, 4, 5],
(n, index) =>
Console.log(`Currently at index ${index}`).pipe(Effect.as(n * 2))
)
Effect.runPromise(result).then(console.log)
// Output:
// Currently at index 0
// Currently at index 1
// Currently at index 2
// Currently at index 3
// Currently at index 4
// [ 2, 4, 6, 8, 10 ]
Example (Running effects without collecting results)
import { Console, Effect } from "effect"
// Apply effects but discard the results
const result = Effect.forEach(
[1, 2, 3, 4, 5],
(n, index) =>
Console.log(`Currently at index ${index}`).pipe(Effect.as(n * 2)),
{ discard: true }
)
Effect.runPromise(result).then(console.log)
// Output:
// Currently at index 0
// Currently at index 1
// Currently at index 2
// Currently at index 3
// Currently at index 4
// undefined
forEach(
var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.entries<Svc>(o: {
[s: string]: Svc;
} | ArrayLike<Svc>): [string, Svc][] (+1 overload)
Returns an array of key/values of the enumerable own properties of an object
entries(peers: Record<string, Svc>peers),
([node: stringnode, service: Svcservice]) =>
import EffectEffect.const map: <Exit.Exit<A, E>, never, never, NodeResult<A, E>>(self: Effect.Effect<Exit.Exit<A, E>, never, never>, f: (a: Exit.Exit<A, E>) => NodeResult<A, E>) => Effect.Effect<NodeResult<A, E>, never, never> (+1 overload)Transforms the value inside an effect by applying a function to it.
When to use
Use to transform an effect's success value with a function that returns a
plain value, producing a new effect without changing the original effect's
typed error or context requirements.
Details
map takes a function and applies it to the value contained within an
effect, creating a new effect with the transformed value.
It's important to note that effects are immutable, meaning that the original
effect is not modified. Instead, a new effect is returned with the updated
value.
Example (Choosing map syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => n + 1
const mappedWithPipe = pipe(myEffect, Effect.map(transformation))
const mappedWithDataFirst = Effect.map(myEffect, transformation)
const mappedWithMethod = myEffect.pipe(Effect.map(transformation))
Example (Adding a service charge)
import { Effect, pipe } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const finalAmount = pipe(
fetchTransactionAmount,
Effect.map(addServiceCharge)
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 101
map(import EffectEffect.const exit: <A, E, never>(
self: Effect.Effect<A, E, never>
) => Effect.Effect<Exit.Exit<A, E>, never, never>
Transforms an effect to encapsulate both failure and success using the Exit
data type.
When to use
Use when you need to inspect the full outcome, including typed failures, defects,
and interruptions.
Details
exit wraps an effect's success or failure inside an Exit type, allowing
you to handle both cases explicitly.
The resulting effect cannot fail because the failure is encapsulated within
the Exit.Failure type. The error type is set to never, indicating that
the effect is structured to never fail directly.
Example (Capturing completion as Exit)
import { Effect } from "effect"
const success = Effect.succeed(42)
const failure = Effect.fail("Something went wrong")
const program1 = Effect.exit(success)
const program2 = Effect.exit(failure)
Effect.runPromise(program1).then(console.log)
// { _id: 'Exit', _tag: 'Success', value: 42 }
Effect.runPromise(program2).then(console.log)
// { _id: 'Exit', _tag: 'Failure', cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong' } }
exit(pick: (service: Svc) => Effect.Effect<A, E>pick(service: Svcservice)), (exit: Exit.Exit<A, E>exit): interface NodeResult<A, E = never>One node's outcome for a gathered query field — node-attributed success/failure.
NodeResult<function (type parameter) A in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>A, function (type parameter) E in <Svc, A, E, B>(peers: Record<string, Svc>, pick: (service: Svc) => Effect.Effect<A, E>, combine: (results: ReadonlyArray<NodeResult<A, E>>) => B): Effect.Effect<B>E> => ({ NodeResult<A, E>.node: stringnode, NodeResult<A, E>.exit: Exit.Exit<A, E>exit })),
{ concurrency?: Concurrency | undefinedconcurrency: "unbounded" },
).Pipeable.pipe<Effect.Effect<NodeResult<A, E>[], never, never>, Effect.Effect<B, never, never>>(this: Effect.Effect<NodeResult<A, E>[], never, never>, ab: (_: Effect.Effect<NodeResult<A, E>[], never, never>) => Effect.Effect<B, never, never>): Effect.Effect<B, never, never> (+21 overloads)pipe(import EffectEffect.const map: <readonly NodeResult<A, E>[], B>(f: (a: readonly NodeResult<A, E>[]) => B) => <E, R>(self: Effect.Effect<readonly NodeResult<A, E>[], E, R>) => Effect.Effect<B, E, R> (+1 overload)Transforms the value inside an effect by applying a function to it.
When to use
Use to transform an effect's success value with a function that returns a
plain value, producing a new effect without changing the original effect's
typed error or context requirements.
Details
map takes a function and applies it to the value contained within an
effect, creating a new effect with the transformed value.
It's important to note that effects are immutable, meaning that the original
effect is not modified. Instead, a new effect is returned with the updated
value.
Example (Choosing map syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => n + 1
const mappedWithPipe = pipe(myEffect, Effect.map(transformation))
const mappedWithDataFirst = Effect.map(myEffect, transformation)
const mappedWithMethod = myEffect.pipe(Effect.map(transformation))
Example (Adding a service charge)
import { Effect, pipe } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const finalAmount = pipe(
fetchTransactionAmount,
Effect.map(addServiceCharge)
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 101
map(combine: (
results: ReadonlyArray<NodeResult<A, E>>
) => B
combine));