Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 2

return vs return await inside try in JavaScript

39 return risky() versus return await risky() inside try

hard

Return a promise from inside a try without awaiting it and watch the rejection walk straight past the catch. Add the await and watch it be caught. Then do the same with finally and find the cleanup that runs before the work has finished.

Stuck? Three hints, smallest first
  1. 1. return somePromise ends the try block immediately. The settlement happens afterwards, outside it.
  2. 2. The value the caller sees is identical either way, which is what makes this so easy to miss.
  3. 3. With finally the bug is louder: the release runs while the work is still going.

The answer

const log = [];

const chargeCard = async () => {
  await Promise.resolve();
  log.push('charge attempted');
  throw new Error('gateway declined');
};

const readReceipt = async () => {
  await Promise.resolve();
  await Promise.resolve();
  log.push('receipt read');
  return 'LAG-0039';
};

const noAwait = async () => {
  try { return chargeCard(); }                 // leaves the try before it settles
  catch { return 'handled here'; }
};

const withAwait = async () => {
  try { return await chargeCard(); }
  catch { return 'handled here'; }
};

const escaped = await noAwait().then((v) => 'resolved: ' + v, (err) => 'ESCAPED: ' + err.message);
const caught = await withAwait();

log.length = 0;
const releaseEarly = async () => {
  try { return readReceipt(); }
  finally { log.push('printer released'); }
};
const earlyValue = await releaseEarly();
const earlyOrder = log.slice();

log.length = 0;
const releaseAfter = async () => {
  try { return await readReceipt(); }
  finally { log.push('printer released'); }
};
const lateValue = await releaseAfter();
const properOrder = log.slice();

log.length = 0;
const finallyOnly = async () => {
  try { return chargeCard(); }
  finally { log.push('released too soon'); }
};
const stillRejects = await finallyOnly().catch((err) => err.message);
const tooSoon = log.slice();

console.log('no await   :', escaped);
console.log('with await :', caught);
console.log('finally    :', earlyOrder, 'vs', properOrder);
console.log('and still rejects:', stillRejects, tooSoon);

Output


      
7 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 7 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

7 checks

test('return without await escapes the catch that was meant to hold it', escaped, 'ESCAPED: gateway declined');
test('return await gives the catch something to catch', caught, 'handled here');
test('the resolved value is identical either way, which is why this hides', [earlyValue, lateValue], ['LAG-0039', 'LAG-0039']);
test('without the await, finally runs before the work has finished', earlyOrder, ['printer released', 'receipt read']);
test('with it, cleanup happens once the value is in hand', properOrder, ['receipt read', 'printer released']);
test('a try with only a finally still lets the rejection through', stillRejects, 'gateway declined');
test('and its cleanup had already run, before the failure even happened', tooSoon, ['released too soon', 'charge attempted']);

return somePromise inside a try completes the try block. The promise is then adopted by the async function's own promise, and it settles later — by which time the catch is long gone, so the rejection goes to the caller. The first two assertions are the same code differing by one keyword and producing opposite outcomes.

What makes this expensive is the third assertion: on the happy path the two versions are indistinguishable, so a missing await passes review, passes tests, and only shows up as an unhandled rejection when the dependency has a bad day. This is why return-await and no-return-await are both real lint rules, and why the modern advice is the boring one — inside a try or try/finally, always return await; outside one, it makes no difference worth arguing about.

With finally the failure is not even conditional. The last two assertions show the release running first, before the work it was protecting has attempted anything: the printer is freed, then the charge happens, then it fails. Connections closed mid-query, transactions committed before the write, spinners hidden while the request is still in flight — all the same missing keyword.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.