Tweakr
    Preparing search index...

    Function once

    • 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.

      Type Parameters

      • T extends (...args: any[]) => any

        The type of the function to be invoked.

      Parameters

      • fn: T

        The function to invoke at most once.

      Returns (...args: Parameters<T>) => ReturnType<T>

      A new function that calls fn only once.

      1.2.0

      const init = once(() => console.log("Initialized"));
      init(); // logs "Initialized"
      init(); // does nothing