What gets logged?
async function f() {
try {
return 'A';
} finally {
return 'B';
}
}
f().then(console.log);
- A
- B
- A then B
- Unhandled promise rejection
BobaMilk ![]()
What gets logged?
async function f() {
try {
return 'A';
} finally {
return 'B';
}
}
f().then(console.log);
BobaMilk ![]()
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.
:: Copyright KIRUPA 2024 //--