Skip to content

Unhandled Promise Rejection

Quick summary

A Promise was rejected (an async operation failed) but you did not handle the error — the rejection is lost.

Why this happens

  • Missing await before an async call that rejects

  • No try/catch around async code that can fail

  • Missing .catch() on a Promise chain

Minimal example

✗ Broken code
async function getUser() {
  const response = await fetch('/api/user'); // May reject!
  return response.json();
}
getUser(); // Unhandled rejection if fetch fails
✓ Fixed code
async function getUser() {
  try {
    const response = await fetch('/api/user');
    return await response.json();
  } catch (error) {
    console.error('Failed:', error.message);
    return null;
  }
}
getUser();

Without try/catch, a failed fetch causes an unhandled rejection. Wrapping the async code in try/catch catches the error and handles it gracefully.

How to diagnose

  • Look for 'UnhandledPromiseRejection' in the error output

  • Check if async functions have try/catch blocks

  • Verify all Promise chains have a .catch()

How to fix

  • Wrap async code in try/catch

  • Add .catch() to Promise chains

  • Use async/await with try/catch for cleaner error handling

How to prevent

  • Always handle async errors with try/catch or .catch()

  • Add a global unhandledRejection handler as a safety net

Related Resources

Related Glossary

Related Lessons

  • Errors

    Learn about async error handling.

← Back to language