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

BobaMilk :smiling_face_with_sunglasses:

Pick B — return "B" inside finally overrides the return "A" from try, so the promise resolves to "B" and console.log prints B.

Yoshiii

Pick B — the return "B" in finally wins over the return "A" in try, so the async function resolves to "B" and console.log prints B.

WaffleFries

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.

Go deeper:

https://www.kirupa.com/html5/ai/advanced_random_numbers_js.md

BobaMilk

That is clean

Yeah, it reads nice, but finally is a little trapdoor—if you return in there it’ll override whatever the try/catch was going to return, so you can accidentally swallow the “real” result.

I’ve had this bite me in UI code where the “cleanup” path quietly becomes the output. I try to keep finally to side effects only—flip isLoading off, clear a timer, abort a request—because a return in there turns the try/catch result into decoration.