# The finally Trap That Silently Breaks Senior JavaScript Code

This is one of my favorite senior-level interview questions. Almost everyone gets it wrong — including people who've been shipping JS for a decade.

## The Problem

```js
async function getUser(id) {
  try {
    return await fetchUser(id);
  } finally {
    return null;
  }
}
```

Two questions:

1.  What does `getUser(1)` return?
    
2.  What happens if `fetchUser` throws?
    

Most seniors say: *"It returns the user, or* `null` *if something fails."*

That's wrong on both counts.

## What Actually Happens

*   If `fetchUser` resolves → `getUser` returns `null`
    
*   If `fetchUser` rejects → `getUser` returns `null` (no error, no throw)
    

The `return` inside `finally` **overrides the** `return` **in** `try` *and* **swallows any exception** thrown by `fetchUser`.

In other words: your error handling is silently dead.

## Why

Per the spec, `finally` runs after `try`/`catch` completes. If `finally` itself returns, that value becomes the function's return value — and any pending completion (a `return`, `throw`, or `break`) from the `try` block is **discarded**.

This isn't a quirk of `async`. It's just less obvious there because you're also awaiting.

```js
function f() {
  try {
    return 1;
  } finally {
    return 2;
  }
}
f(); // 2, not 1
```

## The Fix

Only use `finally` for cleanup. Never `return` from it.

```js
async function getUser(id) {
  try {
    return await fetchUser(id);
  } finally {
    await releaseConnection(); // cleanup only
  }
}
```

If you need to transform the result, do it in `try` or after the `try` block — not in `finally`.

## The Senior-Level Takeaway

`finally` doesn't mean "run this and continue." It means "run this on the way out — and if you complete the function here, you take over." That distinction matters most in async code, where a swallowed error becomes a silent `undefined` three layers up the stack.

This is exactly the kind of thing I put into a **15-question demo test** for senior JavaScript developers — closures, `this`, async, Event Loop, and traps like this one.

👉 **Try the** [**demo test**](https://skillhacker.io/demo/javascript-senior-developer-demo-test)

Short, no fluff, no signup wall.
