Package: effect
Module: Either
The “do simulation” in Effect allows you to write code in a more declarative style, similar to the “do notation” in other programming languages. It provides a way to define variables and perform operations on them using functions like bind and let.
Here’s how the do simulation works:
Do valuebind function to define variables and bind them to Either valuesbind statements to define multiple variables within the scopelet function to define variables and bind them to simple valuesExample
import * as assert from "node:assert"
import { Either, pipe } from "effect"
const result = pipe(
Either.Do,
Either.bind("x", () => Either.right(2)),
Either.bind("y", () => Either.right(3)),
Either.let("sum", ({ x, y }) => x + y)
)
assert.deepStrictEqual(result, Either.right({ x: 2, y: 3, sum: 5 }))
See
DobindToletSignature
declare const bind: { <N extends string, A extends object, B, E2>(name: Exclude<N, keyof A>, f: (a: NoInfer<A>) => Either<B, E2>): <E>(self: Either<A, E>) => Either<{ [K in N | keyof A]: K extends keyof A ? A[K] : B; }, E | E2>; <A extends object, E, N extends string, B, E2>(self: Either<A, E>, name: Exclude<N, keyof A>, f: (a: NoInfer<A>) => Either<B, E2>): Either<{ [K in N | keyof A]: K extends keyof A ? A[K] : B; }, E | E2>; }
Since v2.0.0