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/explanation/sync-and-async.md.

Sync and async Options

An OptionAsync<T> is just an alias for Promise<Option<T>>. An OptionMaybeAsync<T> is the union Option<T> | Promise<Option<T>>. byenull's Promise-aware functions share names across these forms, so you do not need separate map and mapAsync functions.

Use map for synchronous transformations

The callback passed to map transforms a value synchronously. If the input is a Promise of an Option, map waits for that input through its own Promise handling before invoking the synchronous callback.

Use andThen when the next step returns an Option or a Promise of an Option. This includes asynchronous work wrapped with fn:

import { 
import Option
Option
} from "byenull";
declare function
function loadDisplayName(id: string): Promise<string | null>
loadDisplayName
(
id: string
id
: string):
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<string | null>;
const
const displayNameFor: (id: string) => Option.OptionAsync<string>
displayNameFor
=
import Option
Option
.
function fn<(id: string) => Promise<string | null>>(fn: (id: string) => Promise<string | null>): (id: string) => Option.OptionAsync<string> (+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
(
function loadDisplayName(id: string): Promise<string | null>
loadDisplayName
);
const
const pending: Option.OptionAsync<string>
pending
=
import Option
Option
.
function pipe<Option.Option<string>, Option.OptionAsync<string>, Option.OptionAsync<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.OptionAsync<string>, bc: (b: Option.OptionAsync<string>) => 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<string>>(fn: (id: string) => Option.OptionAsync<string>): <O1>(option: O1) => Option.OptionFor<Option.OptionAsync<string> | O1, string> (+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 displayNameFor: (id: string) => Option.OptionAsync<string>
displayNameFor
),
import Option
Option
.
function map<Option.OptionAsync<string>, string>(fn: (a: string) => string): (option: Option.OptionAsync<string>) => 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
((
name: string
name
) =>
name: string
name
.
String.trim(): string

Removes the leading and trailing white space and line terminator characters from a string.

trim
()),
); const
const displayName: Option.Option<string>
displayName
:
import Option
Option
.
type Option<T> = Option.Some<T> | Option.None

A union type representing either a Some or a None .

@typeParamT - The type of the Some value.@example
import { Option } from 'byenull';

const doSomething = (): Option.Option<number> => {
  return Math.random() > 0.5
    ? Option.some(10)
    : Option.none();
};
@categoryCore Types
Option
<string> = await
const pending: Option.OptionAsync<string>
pending
;

displayNameFor returns an OptionAsync<string>. andThen uses that Option as the next pipeline value. The later map callback still receives a resolved string and runs only for Some.

Do not use an async Option-returning callback as a map mapper. map does not flatten that callback's result into the next Option. andThen is the operation for that job.

unwrap distributes over a mixed union

unwrap has a different return-type rule, depending on what the input type is.

For a numeric Option and a numeric fallback, a synchronous input returns number, an asynchronous input returns Promise<number>, and a mixed input returns number | Promise<number>:

import { 
import Option
Option
} from "byenull";
declare const
const mixed: Option.OptionMaybeAsync<number>
mixed
:
import Option
Option
.
type OptionMaybeAsync<T> = Option.Option<T> | Promise<Option.Option<T>>

An option that may be either synchronous or asynchronous.

@typeParamT - The type of the Some value.@categoryCore Types
OptionMaybeAsync
<number>;
const
const value: number | Promise<number>
value
: number |
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<number> =
import Option
Option
.
function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, Option.InferSome<O>>

Extracts the value from an Option or OptionAsync . If the input is a None , the default value is returned.

@typeParamD - The default value type.@example

Some Case

import { Option } from 'byenull';

const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@example

None Case

import { Option } from 'byenull';

const empty: Option.Option<number> = Option.none();

const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@example

Nullish Default

import { Option } from 'byenull';

const value: number | null = Option.pipe(
Option.some(2),
Option.unwrap(null),
);
@seeorElse - For recovering with a fallback Option instead of a plain value.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryUnwraps
unwrap
(0)(
const mixed: Option.OptionMaybeAsync<number>
mixed
);
const
const settledValue: number
settledValue
: number = await
const value: number | Promise<number>
value
;

The runtime behavior matches both members of that return type. A plain Option produces the value or fallback immediately. A Promise input produces a Promise for the value or fallback.

// TODO: Rewrite this to illustrate Pipeline coloring when introducing async steps

The shared API has visible type costs

A separate set of async combinators could give each function a shorter signature. byenull instead uses conditional types such as OptionFor, which are harder to read in editor tooltips and error messages. Mixed-union inputs need particular care because OptionFor can declare a Promise even when one call returns synchronously.

Changing an andThen callback from an Option to an OptionAsync also changes the inferred result of later combinators. Existing pipeline steps can remain unchanged, but callers must await the eventual result when they need a resolved Option.

Use map only for synchronous value transformations. Use andThen or orElse for Option-returning work that may be asynchronous. Await at the point where your code needs one resolved type, especially when the input is OptionMaybeAsync rather than definitely synchronous or definitely asynchronous.