once
once<
T>(fn):T
Defined in: functions/once/index.ts:26
Creates a version of a function that runs only once. Subsequent calls return the cached result from the first invocation and ignore any new arguments.
Type Parameters
Section titled “Type Parameters”T extends (…args) => unknown
Parameters
Section titled “Parameters”T
The function to wrap
Returns
Section titled “Returns”T
A wrapped function that executes fn at most once
Example
Section titled “Example”const init = once(() => ({ id: Math.random() }));const a = init();const b = init();a === b; // trueKeywords
Section titled “Keywords”run once, single call, memoize no args, lazy init, singleton
Throws
Section titled “Throws”Error if fn is not a function
Remarks
Section titled “Remarks”The reference to fn is released after the first call so it can be
garbage-collected. If fn throws on the first call, the exception
propagates and subsequent calls return undefined without retrying.