Create a wrapper around a given function such that it executes at most once. Subsequent calls to the wrapped function return the result from the first execution, regardless of the arguments provided. This behavior is akin to memoization but specifically designed for single-use functions. The result of the first call is stored internally, allowing for efficient retrieval without recomputation.
import*as
import _
_from'radashi'
const
constfn: (this:unknown) =>number
fn=
import _
_.
once<[], number, unknown>(fn: (this:unknown) => number): (this:unknown) => number
export once
Create a function that runs at most once, no matter how many times
it's called. If it was already called before, returns the result
from the first call. This is a lighter version of memo().
To allow your once-wrapped function to be called again, see the
once.reset function.
An intrinsic object that provides basic mathematics functionality and constants.
Math.
Math.random(): number
Returns a pseudorandom number between 0 and 1.
random())
constfn: (this:unknown) =>number
fn() // 0.5
constfn: (this:unknown) =>number
fn() // 0.5
Resetting the function
The once.reset function clears the stored result of a function that was previously wrapped with once. This allows the function to be executed again as if it were never called before, enabling dynamic reuse of the function with fresh computations.
import*as
import _
_from'radashi'
const
constfn: (this:unknown) =>number
fn=
import _
_.
once<[], number, unknown>(fn: (this:unknown) => number): (this:unknown) => number
export once
Create a function that runs at most once, no matter how many times
it's called. If it was already called before, returns the result
from the first call. This is a lighter version of memo().
To allow your once-wrapped function to be called again, see the
once.reset function.
An intrinsic object that provides basic mathematics functionality and constants.
Math.
Math.random(): number
Returns a pseudorandom number between 0 and 1.
random())
constfn: (this:unknown) =>number
fn() // 0.5
constfn: (this:unknown) =>number
fn() // 0.5
import _
_.
constonce:Once
export once
Create a function that runs at most once, no matter how many times
it's called. If it was already called before, returns the result
from the first call. This is a lighter version of memo().
To allow your once-wrapped function to be called again, see the
once.reset function.