import { useRef, useState } from 'preact/hooks' export default function usePromise(fn: () => Promise) { const [result, setResult] = useState(null) const [error, setError] = useState(null) const promiseRef = useRef | null>(null) if (!promiseRef.current) { throw (promiseRef.current = fn().then(setResult, setError)) } else if (result) { return result } else if (error) { throw error } else { throw promiseRef.current } }