Function: bind()

Chains another Result-producing computation and merges its success value into the existing object under the specified key.

  • If the original result is a Failure, it is returned as-is.
  • If the next result is a Failure, it is returned as-is.
  • If both are Success, a new object is returned by shallow-merging: the original success object and { [name]: nextSuccess }.

This is useful for building up objects in a compositional and type-safe way, especially in validation or data-fetching pipelines.

Type Param

The key to assign the result of the fn computation.

Type Param

The input Result or ResultAsync.

Type Param

The result type returned by fn.

Examples

import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.succeed({ name: 'Alice' }),
  Result.bind('age', (user) => Result.succeed(20)),
);
// { type: 'Success', value: { name: 1, age: 20 } }
import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.fail('error'),
  Result.bind('age', (user) => Result.succeed(20)),
);
// { type: 'Failure', error: 'error' }
import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.succeed({ name: 'Alice' }),
  Result.bind('age', (user) => Result.fail('error')),
);
// { type: 'Failure', error: 'error' }

See

pipe - It is recommended to use this function with the pipe function for better readability and composability.

Call Signature

bind<N, R1, R2>(name, fn): (result) => InferSuccess<R1> extends object ? ResultFor<R1 | R2, { [K in string | number | symbol]: K extends keyof InferSuccess<InferSuccess<R1>> ? InferSuccess<InferSuccess<R1>>[K<K>] : InferSuccess<R2> }, InferFailure<R1> | InferFailure<R2>> : unknown

Defined in: functions/bind.ts:64

Type Parameters

N

N extends string

R1

R1 extends ResultMaybeAsync<any, any>

R2

R2 extends ResultMaybeAsync<any, any>

Parameters

name

N

fn

(a) => R2

Returns

(result): InferSuccess<R1> extends object ? ResultFor<R1 | R2, { [K in string | number | symbol]: K extends keyof InferSuccess<InferSuccess<R1>> ? InferSuccess<InferSuccess<R1>>[K<K>] : InferSuccess<R2> }, InferFailure<R1> | InferFailure<R2>> : unknown

Parameters

result

R1

Returns

InferSuccess<R1> extends object ? ResultFor<R1 | R2, { [K in string | number | symbol]: K extends keyof InferSuccess<InferSuccess<R1>> ? InferSuccess<InferSuccess<R1>>[K<K>] : InferSuccess<R2> }, InferFailure<R1> | InferFailure<R2>> : unknown

Call Signature

bind<N, F>(name, fn): <R1>(result) => Parameters<F>[0] extends object ? ResultFor<R1 | ReturnType<F>, { [K in string | number | symbol]: K extends keyof any[any] ? any[any][K<K>] : InferSuccess<F> }, InferFailure<R1> | InferFailure<F>> : unknown

Defined in: functions/bind.ts:65

Type Parameters

N

N extends string

F

F extends (a) => ResultMaybeAsync<any, any>

Parameters

name

N

fn

F

Returns

<R1>(result): Parameters<F>[0] extends object ? ResultFor<R1 | ReturnType<F>, { [K in string | number | symbol]: K extends keyof any[any] ? any[any][K<K>] : InferSuccess<F> }, InferFailure<R1> | InferFailure<F>> : unknown

Type Parameters

R1

R1 extends ResultMaybeAsync<Parameters<F>[0], any>

Parameters

result

R1

Returns

Parameters<F>[0] extends object ? ResultFor<R1 | ReturnType<F>, { [K in string | number | symbol]: K extends keyof any[any] ? any[any][K<K>] : InferSuccess<F> }, InferFailure<R1> | InferFailure<F>> : unknown