Function: andThrough()

Runs an additional computation using the success value of a Result or ResultAsync, but returns the original result if the additional computation is successful.

If either the original result or the side effect result is a Failure, that failure is returned. Useful for running validations or side effects without altering the main result on success.

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(5),
  Result.andThrough((x) => {
    return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
  }),
);
// { type: 'Success', value: 5 }
import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.fail('error),
  Result.andThrough((x) => {
    return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
  }),
);
// { type: 'Failure', error: 'error' }
import { Result } from '@praha/byethrow';

const result = Result.pipe(
  Result.succeed(-10),
  Result.andThrough((x) => {
    return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
  }),
);
// { type: 'Failure', error: 'Must be > 0' }

See

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

Call Signature

andThrough<R1, R2>(fn): (result) => ResultFor<R1 | R2, InferSuccess<R1>, InferFailure<R1> | InferFailure<R2>>

Defined in: functions/and-through.ts:63

Type Parameters

R1

R1 extends ResultMaybeAsync<any, any>

R2

R2 extends ResultMaybeAsync<any, any>

Parameters

fn

(a) => R2

Returns

(result): ResultFor<R1 | R2, InferSuccess<R1>, InferFailure<R1> | InferFailure<R2>>

Parameters

result

R1

Returns

ResultFor<R1 | R2, InferSuccess<R1>, InferFailure<R1> | InferFailure<R2>>

Call Signature

andThrough<F>(fn): <R1>(result) => ResultFor<R1 | ReturnType<F>, InferSuccess<R1>, InferFailure<R1> | InferFailure<F>>

Defined in: functions/and-through.ts:64

Type Parameters

F

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

Parameters

fn

F

Returns

<R1>(result): ResultFor<R1 | ReturnType<F>, InferSuccess<R1>, InferFailure<R1> | InferFailure<F>>

Type Parameters

R1

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

Parameters

result

R1

Returns

ResultFor<R1 | ReturnType<F>, InferSuccess<R1>, InferFailure<R1> | InferFailure<F>>