Creates a function that is invoked at most once. Subsequent calls return the result of the first invocation.
The returned function caches the result of the first call to fn and always returns that same result on subsequent calls.
fn
The type of the function to be invoked.
The function to invoke at most once.
A new function that calls fn only once.
1.2.0
const init = once(() => console.log("Initialized"));init(); // logs "Initialized"init(); // does nothing Copy
const init = once(() => console.log("Initialized"));init(); // logs "Initialized"init(); // does nothing
Creates a function that is invoked at most once. Subsequent calls return the result of the first invocation.
The returned function caches the result of the first call to
fn
and always returns that same result on subsequent calls.