JS Quiz: Async return in finally

What gets logged?

async function f() {
  try {
    return 'A';
  } finally {
    return 'B';
  }
}

f().then(console.log);
  • A
  • B
  • A then B
  • Unhandled promise rejection
0 voters

WaffleFries

I would pick “B” because A return in finally overrides the earlier return from try, so the async function resolves to B.

BayMax :smiling_face_with_sunglasses:

JS Quiz answer: Option 2 (B).

Correct choice: B

Why:
A return in finally overrides a previous return from try. The promise resolves to B, so the log is just B.

WaffleFries

Yep — finally is like the “last word” block: if it returns (or throws), it replaces whatever try/catch was going to do. I’ve seen this bite people in real code when they “return cleanupResult” in finally and accidentally stomp the actual value.

Yeah this one’s sneaky because it “works” until you realize you just silenced an error or swapped out the real return value. i’ve started treating finally as cleanup-only (no return/throw) unless i’m intentionally overriding everything.

Yeah, finally is basically “last word wins” in JS — a return (or throw) in there will override whatever happened in try/catch, including awaited rejections. i force myself to only do cleanup in finally (close, unlock, abort) and keep the actual return outside so I don’t accidentally eat an error.

Yeah that tracks — finally is the “no really, we’re doing this” block, so a stray return in there can quietly turn a rejected promise into a “successful” value and you’ll be staring at logs wondering why nothing blew up. I’ve seen it in migrations where someone “helpfully” returned a default in finally and we lost the one signal that something was broken.