Returning a promise inside try/finally

Returning a promise inside try/finally
Photo by James Ting / Unsplash

Today I learned a quirk of try/finally as it relates to handling of returned promises:
My understanding is was that, in most cases, these are equivalent:

// 1 
const result = await somePromise;
return result;

// 2
return somePromise;

// 3
return await somePromise;

But in the case of a try/finally - they are not:

// 1
try {
  const result = await somePromise;
  return result;
} finally {
  console.log('will aways execute after the promise settles
}

// 2
try {
  return somePromise;
} finally {
  console.log('will race with somePromise and probably execute before the promise settles'
}

// 3
try {
  return await somePromise; 
} finally {
  console.log('same as 1, will always execute after the promise settles')
}

It definitely does make sense, given the whole context, it's just not a context I've hit often (at all?) before. Interestingly, eslint used to have a rule against the use of return await but it was deprecated.

Further reading: https://v8.dev/blog/fast-async