Skip to content

Function API

once Once

Creates a function that runs at most once and caches its first result.

Signature

ts
export function once<This, Arguments extends unknown[], Result>(
	callback: (this: This, ...arguments_: Arguments) => Result
): (this: This, ...arguments_: Arguments) => Result;

Example

ts
import { once } from "@fast-china/utils";

const initialize = once(() => ({ ready: true }));
const result = initialize();

Input

InputTypeRequired / defaultDescription
callback(this: This, ...arguments_: Arguments) => ResultRequiredFunction allowed to execute only once.

Returns

ValueTypeDescription
result(this: This, ...arguments_: Arguments) => ResultWrapper preserving the original parameters and return type.

Synchronous reentry

Reentering the same once wrapper before its first callback returns throws without a second callback execution. The callback may catch that error; otherwise it becomes the cached first synchronous failure. Successful return values and Promise identity remain stable.