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.maybe.md.

Function: maybe()

Executes a function and wraps its result in an Option or OptionAsync.

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

Type Param

F

The function type to execute.

Examples

Synchronous Thunk

import { 
import Option
Option
} from 'byenull';
const
const option: Option.Option<Element>
option
=
import Option
Option
.
function maybe<() => Element | null>(fn: () => Element | null): Option.Option<Element> (+1 overload)

Executes a function and wraps its result in an Option or OptionAsync .

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

@typeParamF - The function type to execute.@returnsAn Option or OptionAsync wrapping the function's return value.@example

Synchronous Thunk

import { Option } from 'byenull';

const option = Option.maybe(() => document.querySelector('#app'));
// { type: 'Some', value: HTMLElement } or { type: 'None' }
@example

Asynchronous Thunk

import { Option } from 'byenull';

type Config = { host: string };

const option = await Option.maybe(() =>
fetch('/api/config').then((response) => response.json() as Promise<Config>),
);
// { type: 'Some', value: Config }
@seefn - For wrapping functions that take parameters.@categoryCreators
maybe
(() =>
var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.
ParentNode.querySelector<Element>(selectors: string): Element | null (+4 overloads)

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

querySelector
('#app'));
// { type: 'Some', value: HTMLElement } or { type: 'None' }

Asynchronous Thunk

import { 
import Option
Option
} from 'byenull';
type
type Config = {
    host: string;
}
Config
= {
host: string
host
: string };
const
const option: Option.Option<any>
option
= await
import Option
Option
.
function maybe<() => Promise<any>>(fn: () => Promise<any>): Option.OptionAsync<any> (+1 overload)

Executes a function and wraps its result in an Option or OptionAsync .

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

@typeParamF - The function type to execute.@returnsAn Option or OptionAsync wrapping the function's return value.@example

Synchronous Thunk

import { Option } from 'byenull';

const option = Option.maybe(() => document.querySelector('#app'));
// { type: 'Some', value: HTMLElement } or { type: 'None' }
@example

Asynchronous Thunk

import { Option } from 'byenull';

type Config = { host: string };

const option = await Option.maybe(() =>
fetch('/api/config').then((response) => response.json() as Promise<Config>),
);
// { type: 'Some', value: Config }
@seefn - For wrapping functions that take parameters.@categoryCreators
maybe
(() =>
function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
fetch
('/api/config').
Promise<Response>.then<Config, any>(onfulfilled?: ((value: Response) => Config | PromiseLike<Config>) | null | undefined, onrejected?: ((reason: any) => any) | null | undefined): Promise<any>

Attaches callbacks for the resolution and/or rejection of the Promise.

@paramonfulfilled The callback to execute when the Promise is resolved.@paramonrejected The callback to execute when the Promise is rejected.@returnsA Promise for the completion of which ever callback is executed.
then
((
response: Response
response
) =>
response: Response
response
.
Body.json(): Promise<any>
json
() as
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<
type Config = {
    host: string;
}
Config
>),
); // { type: 'Some', value: Config }

See

fn - For wrapping functions that take parameters.

Call Signature

maybe<F>(fn): OptionAsync<NonNullable<Awaited<ReturnType<F>>>>

Type Parameters

F

F extends () => Promise<any>

Parameters

fn

F

Returns

OptionAsync<NonNullable<Awaited<ReturnType<F>>>>

Call Signature

maybe<F>(fn): Option<NonNullable<ReturnType<F>>>

Type Parameters

F

F extends () => any

Parameters

fn

F

Returns

Option<NonNullable<ReturnType<F>>>