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
| Input | Type | Required / default | Description |
|---|---|---|---|
callback | (this: This, ...arguments_: Arguments) => Result | Required | Function allowed to execute only once. |
Returns
| Value | Type | Description |
|---|---|---|
result | (this: This, ...arguments_: Arguments) => Result | Wrapper 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.
