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

# Function: fn()

Wraps a function and returns a new function that returns an [Option](/api/types/Option.Option.md) or [OptionAsync](/api/types/Option.OptionAsync.md).

Nullish values and Promise rejections produce [None](/api/types/Option.None.md); synchronous throws are not captured.

## Type Param

**F**

The function type to wrap.

## Examples

**Synchronous Function**

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

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

const findUser = Option.fn((id: string, users: User[]) =>
  users.find((user) => user.id === id),
);

findUser('u1', [{ id: 'u1', name: 'Alice' }]);
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
```

**Asynchronous Function**

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

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

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

await fetchUser('u1');
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
```

## See

[maybe](/api/functions/Option.maybe.md) - For wrapping a thunk that takes no parameters.

## Call Signature

> **fn**\<`F`>(`fn`): (...`args`) => [`OptionAsync`](/api/types/Option.OptionAsync.md)\<`NonNullable`\<`Awaited`\<`ReturnType`\<`F`>>>>

### Type Parameters

#### F

`F` _extends_ (...`args`) => `Promise`\<`any`>

### Parameters

#### fn

`F`

### Returns

(...`args`) => [`OptionAsync`](/api/types/Option.OptionAsync.md)\<`NonNullable`\<`Awaited`\<`ReturnType`\<`F`>>>>

## Call Signature

> **fn**\<`F`>(`fn`): (...`args`) => [`Option`](/api/types/Option.Option.md)\<`NonNullable`\<`ReturnType`\<`F`>>>

### Type Parameters

#### F

`F` _extends_ (...`args`) => `any`

### Parameters

#### fn

`F`

### Returns

(...`args`) => [`Option`](/api/types/Option.Option.md)\<`NonNullable`\<`ReturnType`\<`F`>>>
