import { useState } from "react"; /** * A hook that returns a promise and its state. * * The promise is only created once, and the state is updated when the promise resolves or rejects. * * @param factory A function that returns a promise. * @returns An object containing the promise's state and result. * @example * const { loading, result, error } = usePromise(() => fetch('https://example.com') * .then(response => response.text()) * ); * if (loading) { * return

Loading...

; * } * if (error) { * return

Error: {error.message}

; * } * return

Result: {result}

; */ export function usePromise(factory: () => Promise) { const [loading, setLoading] = useState(true); const [result, setResult] = useState(undefined); const [error, setError] = useState(undefined); const [promise] = useState(() => { const promise = factory(); if (!promise || typeof promise.then !== "function") { throw new Error("The factory function must return a promise."); } return promise .then(setResult) .catch(setError) .finally(() => { setLoading(false); }); }); return { loading, result, error, promise }; }