diff --git a/src/hooks/useAsync.ts b/src/hooks/useAsync.ts index 23499ef..73f0d0c 100644 --- a/src/hooks/useAsync.ts +++ b/src/hooks/useAsync.ts @@ -1,12 +1,44 @@ +/** + * Custom Hook for Async Operations + * Provides a reusable pattern for handling asynchronous operations with loading, error, and data states + */ + import { useState, useCallback } from 'react'; import { handleError } from '../utils/errorHandling'; +/** + * State interface for async operations + * @template T - The type of data returned by the async operation + */ interface AsyncState { + /** The data returned from the async operation, null if not yet loaded or error occurred */ data: T | null; + /** Error object if the operation failed, null otherwise */ error: Error | null; + /** Loading state indicator, true while operation is in progress */ loading: boolean; } +/** + * Custom hook for managing asynchronous operations + * Handles loading states, error handling, and data management for async functions + * + * @template T - The type of data returned by the async operation + * @returns Object containing: + * - data: The result of the async operation or null + * - error: Any error that occurred or null + * - loading: Boolean indicating if operation is in progress + * - execute: Function to trigger the async operation + * + * @example + * ```tsx + * const { data, error, loading, execute } = useAsync(); + * + * useEffect(() => { + * execute(() => fetchUser(userId)); + * }, [userId]); + * ``` + */ export function useAsync() { const [state, setState] = useState>({ data: null,