@vgerbot/async
    Preparing search index...

    Function memoize

    • Creates a memoized version of an async function that caches results. Subsequent calls with the same arguments return the cached result instead of re-executing.

      Type Parameters

      • TArgs extends unknown[]

        The argument types of the function.

      • TResult

        The return type of the function.

      Parameters

      Returns (...args: TArgs) => CancellableHandle<TResult> & { cache: Map<string, TResult> }

      A memoized version of the function with a cache property.

      const expensiveOperation = memoize(
      async (id: number, token: CancellableToken) => {
      await token.sleep(1000);
      return fetchUserData(id);
      }
      );

      const handle1 = expensiveOperation(1); // Takes 1 second
      const handle2 = expensiveOperation(1); // Returns cached result immediately

      // Clear cache
      expensiveOperation.cache.clear();
      // Custom resolver for multiple arguments
      const memoized = memoize(
      async (a: number, b: string) => `${a}-${b}`,
      {
      resolver: (a, b) => `${a}:${b}`,
      }
      );