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 /guide/tutorial/transforming/chaining-options.md.

Chaining options

andThen runs a step that returns another Option. It uses the returned Option directly, so dependent lookups and asynchronous steps can share one pipeline.

Chaining steps with andThen

For a Some, andThen calls the function with the inner value. The function can return either a Some or a None. If the input is already None, the function is not called:

import { 
import Option
Option
} from "byenull";
const
const doubled: Option.Option<number>
doubled
=
import Option
Option
.
function pipe<Option.Option<number>, Option.Option<number>>(a: Option.Option<number>, ab: (a: Option.Option<number>) => Option.Option<number>): Option.Option<number> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function some<number>(value: number): Option.Option<number>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
(3),
import Option
Option
.
function andThen<(value: any) => Option.Option<number>>(fn: (value: any) => Option.Option<number>): <O1>(option: O1) => Option.OptionFor<Option.Option<number> | O1, number> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
((
value: any
value
) =>
import Option
Option
.
function some<number>(value: number): Option.Option<number>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
(
value: any
value
* 2)),
); // { type: 'Some', value: 6 } const
const rejected: Option.Option<never>
rejected
=
import Option
Option
.
function pipe<Option.Option<number>, Option.Option<never>>(a: Option.Option<number>, ab: (a: Option.Option<number>) => Option.Option<never>): Option.Option<never> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function some<number>(value: number): Option.Option<number>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
(3),
import Option
Option
.
function andThen<() => Option.Option<never>>(fn: () => Option.Option<never>): <O1>(option: O1) => Option.OptionFor<Option.Option<never> | O1, never> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
(() =>
import Option
Option
.
function none(): Option.Option<never>

Creates a None : an Option that holds no value.

@returnsA None.@example
import { Option } from 'byenull';

const option = Option.none();
// { type: 'None' }
@categoryCreators
none
()),
); // { type: 'None' } const
const skipped: Option.Option<number>
skipped
=
import Option
Option
.
function pipe<Option.Option<never>, Option.Option<number>>(a: Option.Option<never>, ab: (a: Option.Option<never>) => Option.Option<number>): Option.Option<number> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function none(): Option.Option<never>

Creates a None : an Option that holds no value.

@returnsA None.@example
import { Option } from 'byenull';

const option = Option.none();
// { type: 'None' }
@categoryCreators
none
(),
import Option
Option
.
function andThen<(value: number) => Option.Option<number>>(fn: (value: number) => Option.Option<number>): <O1>(option: O1) => Option.OptionFor<Option.Option<number> | O1, number> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
((
value: number
value
: number) =>
import Option
Option
.
function some<number>(value: number): Option.Option<number>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
(
value: number
value
* 2)),
); // { type: 'None' }, the callback is not called

Unlike map, andThen does not nest an Option returned by its callback. The returned Option becomes the input to the next step.

Chaining sequential lookups

Use andThen when a later lookup needs a value from an earlier one. A missing user or team stops the rest of the chain:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
    teamId: string;
}
User
= {
id: string
id
: string;
teamId: string
teamId
: string };
type
type Team = {
    id: string;
    name: string;
}
Team
= {
id: string
id
: string;
name: string
name
: string };
const
const users: User[]
users
:
type User = {
    id: string;
    teamId: string;
}
User
[] = [{
id: string
id
: "u1",
teamId: string
teamId
: "t1" }];
const
const teams: Team[]
teams
:
type Team = {
    id: string;
    name: string;
}
Team
[] = [{
id: string
id
: "t1",
name: string
name
: "Docs" }];
const
const findUser: (id: string) => Option.Option<User>
findUser
=
import Option
Option
.
function fn<(id: string) => User | undefined>(fn: (id: string) => User | undefined): (id: string) => 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) =>
const 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
const findTeam: (id: string) => Option.Option<Team>
findTeam
=
import Option
Option
.
function fn<(id: string) => Team | undefined>(fn: (id: string) => Team | undefined): (id: string) => Option.Option<Team> (+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) =>
const teams: Team[]
teams
.
Array<Team>.find(predicate: (value: Team, index: number, obj: Team[]) => unknown, thisArg?: any): Team | 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
((
team: Team
team
) =>
team: Team
team
.
id: string
id
===
id: string
id
));
const
const team: Option.Option<Team>
team
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<User>, Option.Option<Team>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<User>, bc: (b: Option.Option<User>) => Option.Option<Team>): Option.Option<Team> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function some<string>(value: string): Option.Option<string>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
("u1"),
import Option
Option
.
function andThen<(id: string) => Option.Option<User>>(fn: (id: string) => Option.Option<User>): <O1>(option: O1) => Option.OptionFor<Option.Option<User> | O1, User> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
(
const findUser: (id: string) => Option.Option<User>
findUser
),
import Option
Option
.
function andThen<(user: any) => Option.Option<Team>>(fn: (user: any) => Option.Option<Team>): <O1>(option: O1) => Option.OptionFor<Option.Option<Team> | O1, Team> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
((
user: any
user
) =>
const findTeam: (id: string) => Option.Option<Team>
findTeam
(
user: any
user
.teamId)),
); // { type: 'Some', value: { id: 't1', name: 'Docs' } } const
const missing: Option.Option<Team>
missing
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<User>, Option.Option<Team>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<User>, bc: (b: Option.Option<User>) => Option.Option<Team>): Option.Option<Team> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function some<string>(value: string): Option.Option<string>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
("u404"),
import Option
Option
.
function andThen<(id: string) => Option.Option<User>>(fn: (id: string) => Option.Option<User>): <O1>(option: O1) => Option.OptionFor<Option.Option<User> | O1, User> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
(
const findUser: (id: string) => Option.Option<User>
findUser
),
import Option
Option
.
function andThen<(user: any) => Option.Option<Team>>(fn: (user: any) => Option.Option<Team>): <O1>(option: O1) => Option.OptionFor<Option.Option<Team> | O1, Team> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
((
user: any
user
) =>
const findTeam: (id: string) => Option.Option<Team>
findTeam
(
user: any
user
.teamId)),
); // { type: 'None' }, the team lookup is not called

Each callback receives a plain value, not an Option. The pipeline handles the wrapping between steps.

Using asynchronous steps

An andThen callback can return an OptionAsync. Later Promise-aware steps wait for it, so map receives the resolved value:

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 | null>>(fn: (id: string) => Promise<User | null>): (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 user: User
user
:
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: "u1",
name: string
name
: "Alice" };
return
id: string
id
===
const user: User
user
.
id: string
id
?
const user: User
user
: null;
}); const
const userName: Option.Option<string>
userName
= await
import Option
Option
.
function pipe<Option.Option<string>, Option.OptionAsync<User>, Option.OptionAsync<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.OptionAsync<User>, bc: (b: Option.OptionAsync<User>) => Option.OptionAsync<string>): Option.OptionAsync<string> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function some<string>(value: string): Option.Option<string>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
("u1"),
import Option
Option
.
function andThen<(id: string) => Option.OptionAsync<User>>(fn: (id: string) => Option.OptionAsync<User>): <O1>(option: O1) => Option.OptionFor<Option.OptionAsync<User> | O1, User> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

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

None Case (fn is never called)

import { Option } from 'byenull';

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

Asynchronous Case

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 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
(
const fetchUser: (id: string) => Option.OptionAsync<User>
fetchUser
),
import Option
Option
.
function map<Option.OptionAsync<User>, string>(fn: (a: User) => string): (option: Option.OptionAsync<User>) => Option.OptionAsync<string> (+1 overload)

Applies a transformation function to the value of an Option or OptionAsync . If the input is a None , it is returned unchanged.

@typeParamO - The input Option or OptionAsync.@typeParamU - The transformed value type.@example

Some Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@example

None Case (unchanged)

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.map((x: number) => x * 10),
);
// { type: 'None' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
map
((
user: User
user
) =>
user: User
user
.
name: string
name
),
); // { type: 'Some', value: 'Alice' }

The fn wrapper converts a nullish resolution or Promise rejection to None. Either outcome skips the following map. Await the pipeline once to get its final Option.

For more on mixing synchronous and asynchronous work, see How to mix sync and async Options and The sync/async model.

andThen does not recover from a None. To continue with a fallback Option, use orElse.