once
Creates a function that is restricted to be called only once. Subsequent calls return the cached result of the first invocation.
The returned function exposes a .reset() method to clear the cache and
allow the original function to be called again.
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn | function | The function to wrap |
Returns
Section titled “Returns”OnceFn<A, R> — A function that invokes fn at most once, with a .reset() method
Examples
Section titled “Examples”Run expensive setup only once
Section titled “Run expensive setup only once”The wrapped function executes only on the first call; all subsequent calls return the cached result.
Guard against multiple event-listener registrations
Section titled “Guard against multiple event-listener registrations”Ensure a side-effecting setup (e.g. addEventListener) runs at most once.
