Hyperlinkv0.8.0-beta.28

Result

Result.bindconsteffect/Result.ts:1655
<N extends string, A extends object, B, L2>(
  name: Exclude<N, keyof A>,
  f: (a: NoInfer<A>) => Result<B, L2>
): <L1>(
  self: Result<A, L1>
) => Result<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, L1 | L2>
<A extends object, L1, N extends string, B, L2>(
  self: Result<A, L1>,
  name: Exclude<N, keyof A>,
  f: (a: NoInfer<A>) => Result<B, L2>
): Result<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, L1 | L2>

Adds a named field to the do-notation accumulator by running a Result-producing function that receives the current accumulated object.

When to use

Use when you need to add a Result-producing step to a Result do-notation pipeline and store its successful value under a named field in the accumulated object.

Details

  • Short-circuits on the first Failure
  • The field name must not collide with existing keys
  • Use let for pure (non-Result) computed fields

Example (Binding Result values)

import { pipe, Result } from "effect"

const result = pipe(
  Result.Do,
  Result.bind("x", () => Result.succeed(2)),
  Result.bind("y", ({ x }) => Result.succeed(x + 3))
)
console.log(result)
// Output: { _tag: "Success", success: { x: 2, y: 5 }, ... }
do notationlet_DobindTo
Source effect/Result.ts:165511 lines
export const bind: {
  <N extends string, A extends object, B, L2>(
    name: Exclude<N, keyof A>,
    f: (a: NoInfer<A>) => Result<B, L2>
  ): <L1>(self: Result<A, L1>) => Result<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, L1 | L2>
  <A extends object, L1, N extends string, B, L2>(
    self: Result<A, L1>,
    name: Exclude<N, keyof A>,
    f: (a: NoInfer<A>) => Result<B, L2>
  ): Result<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, L1 | L2>
} = doNotation.bind<ResultTypeLambda>(map, flatMap)