Course contentsShow
← JavaScript
Lesson 1389 of 2,72330. Async/AwaitFree lesson

Returning vs Awaiting in Finally

Learn how returning in a 'finally' block can suppress errors and override resolved values unexpectedly.

Returning vs Awaiting in Finally

You'll learn why returning a value from a finally block can unexpectedly hide errors and override your function's intended result.

The Unexpected Override

In JavaScript, a return statement inside a finally block has special—and potentially dangerous—behavior. It overrides whatever the function was supposed to return, including both successful values and errors.

When you return a value (or even return nothing) from finally, it suppresses any error that was being thrown and replaces the resolved value from your async function. This happens because finally executes after the try or catch blocks, and a return in finally takes precedence over everything.

The Problem in Action

async function problematic() {
  try {
    throw new Error('Something broke!');
  } finally {
    return 'All good!'; // ❌ Hides the error!
  }
}

problematic().then(result => console.log(result)); // 'All good!'
// The error vanished!

The error was thrown, but returning from finally made it disappear completely.

The Safe Approach

If you need to perform async cleanup in finally, use await but don't return:

async function safer() {
  try {
    throw new Error('Something broke!');
  } finally {
    await cleanup(); // ✅ Await, but don't return
  }
}

safer().catch(err => console.log(err.message)); // 'Something broke!'
// Error propagates correctly

Key Takeaway: Never return a value from a finally block in async functions—it silently suppresses errors and overrides resolved values. Use await for cleanup operations, but let errors and return values propagate naturally.