> For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt.

# Function: orThrough()

Runs an additional computation using the error value of a [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md),
but **returns the original failure** if the additional computation is successful.

If the original result is a [Success](/byethrow/api/types/Result.Success.md), it is returned immediately without running the function.
If the original result is a [Failure](/byethrow/api/types/Result.Failure.md), the function is executed with the error value.
If the function returns a [Success](/byethrow/api/types/Result.Success.md), the original failure is returned.
If the function returns a [Failure](/byethrow/api/types/Result.Failure.md), that new failure is returned.

Useful for running error recovery or fallback logic while preserving the original error on success.

## Type Param

**R1**

The input [Result](/byethrow/api/types/Result.Result.md) or [ResultAsync](/byethrow/api/types/Result.ResultAsync.md).

## Type Param

**R2**

The result type returned by `fn`.

## Examples

**Success Case**

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

const result = Result.pipe(
  Result.succeed(5),
  Result.orThrough((error) => {
    return Result.succeed(null);
  }),
);
// { type: 'Success', value: 5 }
```

**Failure Case (function returns a Success)**

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

const result = Result.pipe(
  Result.fail('error'),
  Result.orThrough((error) => {
    console.log('Logging error:', error);
    return Result.succeed(null);
  }),
);
// { type: 'Failure', error: 'error' }
```

**Failure Case (function returns a Failure)**

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

const result = Result.pipe(
  Result.fail('original error'),
  Result.orThrough((error) => {
    return Result.fail('new error');
  }),
);
// { type: 'Failure', error: 'new error' }
```

## See

[pipe](/byethrow/api/functions/Result.pipe.md) - It is recommended to use this function with the [pipe](/byethrow/api/functions/Result.pipe.md) function for better readability and composability.

## Call Signature

> **orThrough**\<`R1`, `R2`>(`fn`): (`result`) => [`ResultFor`](/byethrow/api/types/Result.ResultFor.md)\<`R1` | `R2`, [`InferSuccess`](/byethrow/api/types/Result.InferSuccess.md)\<`R1`>, [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`R2`>>

### Type Parameters

#### R1

`R1` _extends_ [`ResultMaybeAsync`](/byethrow/api/types/Result.ResultMaybeAsync.md)\<`any`, `any`>

#### R2

`R2` _extends_ [`ResultMaybeAsync`](/byethrow/api/types/Result.ResultMaybeAsync.md)\<`any`, `any`>

### Parameters

#### fn

(`a`) => `R2`

### Returns

(`result`) => [`ResultFor`](/byethrow/api/types/Result.ResultFor.md)\<`R1` | `R2`, [`InferSuccess`](/byethrow/api/types/Result.InferSuccess.md)\<`R1`>, [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`R2`>>

## Call Signature

> **orThrough**\<`F`>(`fn`): \<`R1`>(`result`) => [`ResultFor`](/byethrow/api/types/Result.ResultFor.md)\<`R1` | `ReturnType`\<`F`>, [`InferSuccess`](/byethrow/api/types/Result.InferSuccess.md)\<`R1`>, [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`F`>>

### Type Parameters

#### F

`F` _extends_ (`a`) => [`ResultMaybeAsync`](/byethrow/api/types/Result.ResultMaybeAsync.md)\<`any`, `any`>

### Parameters

#### fn

`F`

### Returns

\<`R1`>(`result`) => [`ResultFor`](/byethrow/api/types/Result.ResultFor.md)\<`R1` | `ReturnType`\<`F`>, [`InferSuccess`](/byethrow/api/types/Result.InferSuccess.md)\<`R1`>, [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`R1`> | [`InferFailure`](/byethrow/api/types/Result.InferFailure.md)\<`F`>>
