common-utility-functions
    Preparing search index...

    Function debounce

    • Creates a debounced function that delays invoking the provided function until after a certain number of milliseconds have passed since the last time it was invoked. The debounced function returns a promise that resolves with the result of the last invocation or rejects if the invocation throws an error.

      Type Parameters

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

        A generic type that extends a function with any number of arguments and any return type.

      Parameters

      • func: T

        The function to debounce.

      • delay: number

        The number of milliseconds to delay.

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

      A new function that delays execution and returns a promise.

      const expensiveOperation = async (value: string) => {
      if (value === 'error') {
      throw new Error('Operation failed');
      }
      console.log(`Operating on: ${value}`);
      return value.toUpperCase();
      };

      const debouncedOperation = debounce(expensiveOperation, 500);

      // This will resolve with "SUCCESS"
      debouncedOperation('success').then(console.log);

      // This will reject with an error
      debouncedOperation('error').catch(console.error);

      1.0.0