interface AsyncState { isLoading: boolean; error: Error | null; value: T | null; } /** * Hook that manages async operations with loading, error, and success states. * * Provides a clean interface for handling asynchronous functions with automatic * state management for loading indicators and error handling. * * @template T - The type of data returned by the async function * @param asyncFunction - The async function to execute * * @returns An object containing: * - `execute`: Function to trigger the async operation * - `status`: Current state with `isLoading`, `error`, and `value` properties * * @example * ```tsx * const fetchUser = async (id: string) => { * const response = await fetch(`/api/users/${id}`); * return response.json(); * }; * * const { execute, status } = useAsync(fetchUser); * * // In your component * if (status.isLoading) return
Loading...
; * if (status.error) return
Error: {status.error.message}
; * if (status.value) return
User: {status.value.name}
; * * // Trigger the async operation * * ``` * * @see https://thibault.sh/hooks/use-async */ declare function useAsync(asyncFunction: (...args: any[]) => Promise): { execute: (...args: any[]) => Promise; status: AsyncState; }; export { useAsync };