#Function: andThen()
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.
#Type Param
F
The function type to chain.
#Examples
Some Case
import { import Option Option } from 'byenull';
const const option: Option.Option<number> option = 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.@exampleimport { 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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (2),
import Option Option .function andThen<(x: any) => Option.Option<number>>(fn: (x: 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.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@exampleNone 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' }
@exampleAsynchronous 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 ((x: any x ) => 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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (x: any x * 2)),
);
// { type: 'Some', value: 4 }None Case (fn is never called)
import { import Option Option } from 'byenull';
const const option: Option.Option<number> option = 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.@exampleimport { 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.@exampleimport { Option } from 'byenull';
const option = Option.none();
// { type: 'None' }
@categoryCreators none (),
import Option Option .function andThen<(x: number) => Option.Option<number>>(fn: (x: 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.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@exampleNone 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' }
@exampleAsynchronous 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 ((x: number x : 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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (x: number x * 2)),
);
// { type: 'None' }Asynchronous Case
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.@exampleSynchronous 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' } }
@exampleAsynchronous 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) =>
function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> fetch (`/api/users/${id: string id }`).Promise<Response>.then<User, never>(onfulfilled?: ((value: Response) => User | PromiseLike<User>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<User>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 User = {
id: string;
name: string;
}
User >),
);
const const option: Option.Option<User> option = await import Option Option .function pipe<Option.Option<string>, Option.OptionAsync<User>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.OptionAsync<User>): Option.OptionAsync<User> (+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.@exampleimport { 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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some ('u123'),
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.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@exampleNone 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' }
@exampleAsynchronous 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 ),
);
// { type: 'Some', value: User }#See
pipe - It is recommended to use this function with the pipe function for better readability and composability.
#Call Signature
andThen<
F>(fn): <O1>(option) =>OptionFor<O1|ReturnType<F>,InferSome<F>>
#Type Parameters
#F
F extends (a) => OptionMaybeAsync<any>
#Parameters
#fn
F
#Returns
<O1>(option) => OptionFor<O1 | ReturnType<F>, InferSome<F>>
#Call Signature
andThen<
O1,O2>(fn): (option) =>OptionFor<O1|O2,InferSome<O2>>
#Type Parameters
#O1
O1 extends OptionMaybeAsync<any>
#O2
O2 extends OptionMaybeAsync<any>
#Parameters
#fn
(a) => O2