Skip to the page

async/await exactly · mid

Error handling with async/await (return vs return await)

Inside an async function, try/catch catches a rejection only if you await it inside the try block — return promise without await hands the promise out before the try ends — and every promise you start must eventually be awaited or get a .catch, or it becomes an unhandled rejection.

The notes

What you must know

  • try { await p } catch works. try { return p } lets the rejection escape the local catch. Use return await p inside try, catch or finally.
  • Promises you start and don't await need a .catch; otherwise you get 'unhandledrejection' in browsers or a crash in Node.
  • await Promise.all([...]) inside try throws on the first rejection while the others keep running; use allSettled when partial results are fine.
  • Errors thrown inside callbacks (setTimeout, forEach with an async callback) are not caught by the outer try.
  • fetch doesn't reject on HTTP errors: check res.ok and throw. Handle JSON parse errors. Treat AbortError separately and quietly.
  • Use finally for cleanup such as clearing a loading flag.

The red pen

Common traps: mistake → fix

  • Mistake: return inside try is enough

    Fix: you need return await for the local catch to see the rejection.

  • Mistake: An await inside forEach is awaited by the outer function

    Fix: forEach ignores the returned promises.

  • Mistake: Unhandled rejections are only warnings

    Fix: Node exits by default; browsers log them and fire an event.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you handle errors with async/await?
  2. return vs return await inside try/catch: predict the output.
  3. What happens to an unhandled rejection in the browser vs Node?
  4. Partial failure: Promise.all vs allSettled.
  5. Design a fetch wrapper with typed errors, a timeout and retry.

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview