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

Function: orElse()

orElse<F>(fn): <O1>(option) => OptionFor<O1 | ReturnType<F>, InferSome<O1> | InferSome<F>>

Chains a fallback computation for an Option or OptionAsync. If the input is a Some, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

Type Parameters

F

F extends () => OptionMaybeAsync<any>

The fallback function type.

Parameters

fn

F

Returns

<O1>(option) => OptionFor<O1 | ReturnType<F>, InferSome<O1> | InferSome<F>>

Examples

None Case

import { 
import Option
Option
} from 'byenull';
const
const option: Option.Option<string>
option
=
import Option
Option
.
function pipe<Option.Option<never>, Option.Option<string>>(a: Option.Option<never>, ab: (a: Option.Option<never>) => Option.Option<string>): Option.Option<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 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 orElse<() => Option.Option<string>>(fn: () => Option.Option<string>): <O1>(option: O1) => Option.OptionFor<Option.Option<string> | O1, string | Option.InferSome<O1>>

Chains a fallback computation for an Option or OptionAsync . If the input is a Some , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The fallback function type.@example

None Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.orElse(() => Option.some('default')),
);
// { type: 'Some', value: 'default' }
@example

Some Case (fn is never called)

import { Option } from 'byenull';

const option = Option.pipe(
Option.some('value'),
Option.orElse(() => Option.some('default')),
);
// { type: 'Some', value: 'value' }
@seeunwrap - For returning a plain value instead of a fallback Option.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
orElse
(() =>
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
('default')),
); // { type: 'Some', value: 'default' }

Some Case (fn is never called)

import { 
import Option
Option
} from 'byenull';
const
const option: Option.Option<string>
option
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<string>): Option.Option<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
('value'),
import Option
Option
.
function orElse<() => Option.Option<string>>(fn: () => Option.Option<string>): <O1>(option: O1) => Option.OptionFor<Option.Option<string> | O1, string | Option.InferSome<O1>>

Chains a fallback computation for an Option or OptionAsync . If the input is a Some , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The fallback function type.@example

None Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.orElse(() => Option.some('default')),
);
// { type: 'Some', value: 'default' }
@example

Some Case (fn is never called)

import { Option } from 'byenull';

const option = Option.pipe(
Option.some('value'),
Option.orElse(() => Option.some('default')),
);
// { type: 'Some', value: 'value' }
@seeunwrap - For returning a plain value instead of a fallback Option.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
orElse
(() =>
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
('default')),
); // { type: 'Some', value: 'value' }

See

  • unwrap - For returning a plain value instead of a fallback Option.
  • pipe - It is recommended to use this function with the pipe function for better readability and composability.