import { useState } from 'react'; export type UseAsyncResult = [ T | undefined, (...args: any[]) => Promise, boolean, undefined | Error, ]; export type UseDeleteResult = [ (...args: any[]) => Promise, boolean, undefined | Error, ]; export const useAsync = ( method: (...args: any[]) => Promise, ): UseAsyncResult => { const [data, setData] = useState(); const [error, setError] = useState(); const [loading, setLoading] = useState(false); const fetch = async (...opts: any[]) => { setLoading(true); setError(undefined); try { const res = await method(...opts); setData(res); } catch (err) { setData(undefined); setError(err); } finally { setLoading(false); } }; return [data, fetch, loading, error]; };