/** * Return type for the useAsync hook */ export interface UseAsyncReturn { /** * Execute the async function with the provided arguments */ execute: (...args: Args) => Promise; /** * Whether the async operation is currently in progress */ loading: boolean; /** * Error from the last failed async operation, or null if no error */ error: Error | null; /** * Data from the last successful async operation, or null if not yet executed */ data: T | null; /** * Reset the hook state to initial values */ reset: () => void; } /** * Manages the state and lifecycle of async operations. * Provides loading, error, and data states, with the ability to execute and reset. * * @template T - The return type of the async function * @template Args - The argument types of the async function * @param asyncFunction - The async function to manage * @returns Object containing execute function and state values * * @example * ```tsx * function UserProfile({ userId }: { userId: string }) { * const { execute, loading, error, data } = useAsync(fetchUser); * * useEffect(() => { * execute(userId); * }, [userId, execute]); * * if (loading) return ; * if (error) return ; * if (!data) return null; * * return
{data.name}
; * } * ``` * * @example * ```tsx * function SearchComponent() { * const [query, setQuery] = useState(''); * const { execute, loading, error, data, reset } = useAsync(searchAPI); * * const handleSearch = async () => { * if (query.trim()) { * await execute(query); * } * }; * * const handleClear = () => { * setQuery(''); * reset(); * }; * * return ( *
* setQuery(e.target.value)} /> * * * {error && } * {data && } *
* ); * } * ``` * * @example * ```tsx * function FormSubmission() { * const { execute, loading, error, data } = useAsync(submitForm); * const [formData, setFormData] = useState({ name: '', email: '' }); * * const handleSubmit = async (e: React.FormEvent) => { * e.preventDefault(); * const result = await execute(formData); * if (result) { * console.log('Form submitted successfully:', result); * } * }; * * return ( *
* setFormData({ ...formData, name: e.target.value })} * /> * setFormData({ ...formData, email: e.target.value })} * /> * * {error &&
{error.message}
} * {data &&
Success!
} *
* ); * } * ``` */ export declare function useAsync(asyncFunction: (...args: Args) => Promise): UseAsyncReturn; /** * Alternative version that immediately executes the async function on mount. * Useful for fetching data when a component mounts. * * @template T - The return type of the async function * @param asyncFunction - The async function to execute * @returns Object containing loading, error, data, and refetch function * * @example * ```tsx * function Dashboard() { * const { loading, error, data, refetch } = useAsyncImmediate(() => * fetchDashboardData() * ); * * if (loading) return ; * if (error) return ; * * return ( *
* * *
* ); * } * ``` */ export declare function useAsyncImmediate(asyncFunction: () => Promise): { loading: boolean; error: Error | null; data: T | null; refetch: () => Promise; }; //# sourceMappingURL=useAsync.d.ts.map