Uncaught (in promise) means a promise rejected and nothing handled it. Learn where to put catch, how try and await work together, and how to log unhandled rejections.
The short answer
#A promise rejected and no code attached a handler for it. Add a .catch() to the end of the chain, or wrap the await in try...catch. Make sure every promise you start is either awaited, returned, or given a handler.
_10try {_10 const response = await fetch('/api/data');_10 if (!response.ok) throw new Error(`HTTP ${response.status}`);_10 const data = await response.json();_10 render(data);_10} catch (error) {_10 showError(error);_10}
Why it happens
#When a promise rejects, the error travels down the chain until it meets a rejection handler. If it reaches the end of the chain with no handler, the browser logs "Uncaught (in promise)" and Node.js emits an unhandledRejection event. In current Node.js releases the default is to crash the process for unhandled rejections.
A normal try...catch only catches errors thrown while the try block runs. A promise that rejects later is not caught unless you await it inside the block. Common causes are:
- Calling an async function without
awaitor.catch(), often called a floating promise. - Wrapping a
.then()chain intry...catchwithout awaiting it. - A
.catch()handler that throws again, or a.then()placed after the.catch()that throws. - Starting several promises and awaiting only the first, so a later one rejects with nothing listening.
How to fix it
#Await inside try...catch
This is the most readable option for async functions. The await is what lets catch see the rejection.
_10async function save(item) {_10 try {_10 await api.save(item);_10 } catch (error) {_10 console.error('Save failed', error);_10 throw error; // rethrow if the caller should know_10 }_10}
End promise chains with .catch()
When you use .then(), put a single .catch() at the end so it handles a rejection from any step.
_10loadUser(id)_10 .then((user) => loadPosts(user.id))_10 .then(renderPosts)_10 .catch(showError);
Handle fire-and-forget calls
If you deliberately do not await something, such as analytics in an event handler, still attach a handler.
_10button.addEventListener('click', () => {_10 trackClick().catch((error) => console.warn('Tracking failed', error));_10 openMenu();_10});
Handle every promise you start in parallel
If you start several requests, await them together so any rejection is caught. Use Promise.allSettled when you want every result even if some fail.
_10const [user, settings] = await Promise.all([loadUser(), loadSettings()]);
Add a global safety net for logging
Global handlers are for reporting, not for normal error handling. They tell you what you missed.
_10// Browser_10window.addEventListener('unhandledrejection', (event) => {_10 reportError(event.reason);_10});_10_10// Node.js_10process.on('unhandledRejection', (reason) => {_10 console.error('Unhandled rejection', reason);_10 process.exitCode = 1;_10});
Common pitfalls
#- Assuming
fetchrejects on 404 or 500. It only rejects on network failure; checkresponse.okyourself. - Rejecting with a string instead of an
Error. You lose the stack trace that tells you where it came from. - Catching and doing nothing. An empty
catchhides real bugs; at least log the error. - Returning inside
trywithoutawait, as inreturn fetchData(). The rejection skips yourcatch; usereturn await fetchData()when you want it caught there. - Using a global handler instead of local ones. By then you have lost the context to recover.
Related reading
#Was this resource helpful?