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

# Function: andThen()

Chains the next computation using the value of an [Option](/api/types/Option.Option.md) or [OptionAsync](/api/types/Option.OptionAsync.md).
If the input is a [None](/api/types/Option.None.md), it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.

## Type Param

**F**

The function type to chain.

## Examples

**Some Case**

```ts
import { Option } from 'byenull';

const option = Option.pipe(
  Option.some(2),
  Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
```

**None Case (fn is never called)**

```ts
import { Option } from 'byenull';

const option = Option.pipe(
  Option.none(),
  Option.andThen((x: number) => Option.some(x * 2)),
);
// { type: 'None' }
```

**Asynchronous Case**

```ts
import { Option } from 'byenull';

type User = { id: string; name: string };

const fetchUser = Option.fn(async (id: string) =>
  fetch(`/api/users/${id}`).then((response) => response.json() as Promise<User>),
);

const option = await Option.pipe(
  Option.some('u123'),
  Option.andThen(fetchUser),
);
// { type: 'Some', value: User }
```

## See

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

## Call Signature

> **andThen**\<`F`>(`fn`): \<`O1`>(`option`) => [`OptionFor`](/api/types/Option.OptionFor.md)\<`O1` | `ReturnType`\<`F`>, [`InferSome`](/api/types/Option.InferSome.md)\<`F`>>

### Type Parameters

#### F

`F` _extends_ (`a`) => [`OptionMaybeAsync`](/api/types/Option.OptionMaybeAsync.md)\<`any`>

### Parameters

#### fn

`F`

### Returns

\<`O1`>(`option`) => [`OptionFor`](/api/types/Option.OptionFor.md)\<`O1` | `ReturnType`\<`F`>, [`InferSome`](/api/types/Option.InferSome.md)\<`F`>>

## Call Signature

> **andThen**\<`O1`, `O2`>(`fn`): (`option`) => [`OptionFor`](/api/types/Option.OptionFor.md)\<`O1` | `O2`, [`InferSome`](/api/types/Option.InferSome.md)\<`O2`>>

### Type Parameters

#### O1

`O1` _extends_ [`OptionMaybeAsync`](/api/types/Option.OptionMaybeAsync.md)\<`any`>

#### O2

`O2` _extends_ [`OptionMaybeAsync`](/api/types/Option.OptionMaybeAsync.md)\<`any`>

### Parameters

#### fn

(`a`) => `O2`

### Returns

(`option`) => [`OptionFor`](/api/types/Option.OptionFor.md)\<`O1` | `O2`, [`InferSome`](/api/types/Option.InferSome.md)\<`O2`>>
