
Digiaru started this conversation 1 week ago.
Unhandled promise rejection from async function used in React useEffect
In a React useEffect, I call an async function but forget to await or return it. If it throws, I get an unlogged rejection or erratic component behavior.
Kar
Posted 1 week ago
An async function without await inside useEffect runs synchronously up to its first await; errors inside it become unhandled if not caught ([turn0reddit27]). Fixes: • Wrap async call with a promise and catch: js Copy code useEffect(() => { (async () => { try { await load(); } catch(e) { console.error(e); } })(); }, []);