R
fn

once

v0.0.10tested

Wraps a function so it runs at most once; subsequent calls return the first result

Example

ts
const init = once(() => Math.random());
init(); // 0.42
init(); // 0.42 (cached)
init.clear();
init(); // 0.91 (runs again)

Signature

ts
export function once<T extends AnyFunction>(fn: T): OnceFunction<T>{ ... }

Type Parameters

Textends AnyFunction

Parameters

ParameterTypeDescription
fnTThe function to guard

Returns

OnceFunction<T>The guarded function with a `clear()` method to reset it