Lingua-e
JavaScript

What is a UnhandledPromiseRejection?

← Errors

An UnhandledPromiseRejection occurs when a Promise is rejected but no .catch() handler or try/catch inside an async function catches the error. In Node.js it will crash the process by default in recent versions. In browsers it fires the unhandledrejection event.

Why it happens

  • Calling an async function without awaiting it or attaching a .catch() handler.
  • An awaited call throws inside an async function that has no surrounding try/catch.
  • A Promise chain that throws in a .then() callback but the chain has no .catch() at the end.
  • Forgetting to return a Promise from a function, leaving a floating promise in a chain.

Examples

JavaScript

Code that triggers the error

async function fetchUser(id) {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json();
}

// Called without catch: rejection is unhandled
fetchUser(999);

Error output

UnhandledPromiseRejection: Error: HTTP 404

How to fix it

Always attach a .catch() to every Promise chain, or use try/catch inside async functions. Set up a global handler (process.on('unhandledRejection', ...) in Node.js or window.addEventListener('unhandledrejection', ...) in the browser) as a safety net to log unexpected rejections. Never leave a floating promise that discards its result.

Fixed code

async function fetchUser(id) {
  const res = await fetch(`/api/users/${id}`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json();
}

// Always handle the rejection
fetchUser(999).catch((err) => {
  console.error("Failed to fetch user:", err.message);
});

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free