/** * Creates a debounced function that delays invoking the provided function * until after the specified wait time has elapsed since the last time it was invoked. * Properly handles async functions by unwrapping the Promise. * * @param func - The function to debounce (can be async) * @param wait - The number of milliseconds to delay * @returns A debounced version of the function */ export function debounce Promise>( func: T, wait: number ): (...args: Parameters) => ReturnType { let timeout: NodeJS.Timeout | null = null; return function(...args: Parameters): ReturnType { return new Promise((resolve) => { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(async () => { const result = await func(...args); resolve(result); }, wait); }) as ReturnType; }; }