JS Quiz: Easy: 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
1 Like

It logs B. A return inside finally overrides whatever happened in the try (even a return 'A'), so the async function’s promise resolves with 'B' here.

This “finally wins” behavior is kind of scary tbh because it can hide errors too if you throw in try but return in finally.

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

Ellen