For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/functions/Option.fn.md.

Function: fn()

Wraps a function and returns a new function that returns an Option or OptionAsync.

Nullish values and Promise rejections produce None; synchronous throws are not captured.

Type Param

F

The function type to wrap.

Examples

Synchronous Function

import { 
import Option
Option
} from 'byenull';
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
const
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
=
import Option
Option
.
function fn<(id: string, users: User[]) => User | undefined>(fn: (id: string, users: User[]) => User | undefined): (id: string, users: User[]) => Option.Option<User> (+1 overload)

Wraps a function and returns a new function that returns an Option or OptionAsync .

Nullish values and Promise rejections produce None ; synchronous throws are not captured.

@typeParamF - The function type to wrap.@returnsA new function that returns an Option or OptionAsync.@example

Synchronous Function

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' } }
@example

Asynchronous Function

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' } }
@seemaybe - For wrapping a thunk that takes no parameters.@categoryCreators
fn
((
id: string
id
: string,
users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[]) =>
users: User[]
users
.
Array<User>.find(predicate: (value: User, index: number, obj: User[]) => unknown, thisArg?: any): User | undefined (+1 overload)

Returns the value of the first element in the array where predicate is true, and undefined otherwise.

@parampredicate find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.@paramthisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
find
((
user: User
user
) =>
user: User
user
.
id: string
id
===
id: string
id
),
);
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
('u1', [{
id: string
id
: 'u1',
name: string
name
: 'Alice' }]);
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }

Asynchronous Function

import { 
import Option
Option
} from 'byenull';
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
const
const fetchUser: (id: string) => Option.OptionAsync<User>
fetchUser
=
import Option
Option
.
function fn<(id: string) => Promise<User>>(fn: (id: string) => Promise<User>): (id: string) => Option.OptionAsync<User> (+1 overload)

Wraps a function and returns a new function that returns an Option or OptionAsync .

Nullish values and Promise rejections produce None ; synchronous throws are not captured.

@typeParamF - The function type to wrap.@returnsA new function that returns an Option or OptionAsync.@example

Synchronous Function

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' } }
@example

Asynchronous Function

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' } }
@seemaybe - For wrapping a thunk that takes no parameters.@categoryCreators
fn
(async (
id: string
id
: string) => {
const
const response: Response
response
= await
function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
fetch
(`/api/users/${
id: string
id
}`);
return
const response: Response
response
.
Body.json(): Promise<any>
json
() as
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<
type User = {
    id: string;
    name: string;
}
User
>;
}); await
const fetchUser: (id: string) => Option.OptionAsync<User>
fetchUser
('u1');
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }

See

maybe - For wrapping a thunk that takes no parameters.

Call Signature

fn<F>(fn): (...args) => OptionAsync<NonNullable<Awaited<ReturnType<F>>>>

Type Parameters

F

F extends (...args) => Promise<any>

Parameters

fn

F

Returns

(...args) => OptionAsync<NonNullable<Awaited<ReturnType<F>>>>

Call Signature

fn<F>(fn): (...args) => Option<NonNullable<ReturnType<F>>>

Type Parameters

F

F extends (...args) => any

Parameters

fn

F

Returns

(...args) => Option<NonNullable<ReturnType<F>>>