DEV Community

Cover image for Finally in Promises & Try/Catch

Finally in Promises & Try/Catch

Anna Rankin on September 19, 2018

Lately, I've been experimenting more with the async/await keywords in JavaScript. I noticed that I sometimes struggle to reconcile the strategies I...
Collapse
 
ben profile image
Ben Halpern

Finally, I pinged my tech lead detailing my annoyance

Collapse
 
alainvanhout profile image
Alain Van Hout

Good catch!

Collapse
 
annarankin profile image
Anna Rankin

😜heeheehee

Collapse
 
functionalstoic profile image
JasonSooter

You gave it the good old college try with that one!

Collapse
 
frankrobert profile image
Frank Robert

Great write-up! I've only ever found a single good use for finally. You could do performance checks using console.time.

// logs async evaluation in ms
const promises = [new Promise(), new Promise()];

for (const promise of promises) {
  console.time('timer');
  try {
    await promise();
  } catch(error) {
    console.error(error);
  } finally {
    console.timeEnd('timer');
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mcstepp profile image
Meg Stepp

I also use finally to toggle loading indicators to keep things DRY in my then/catches. If I'm doing the same line of code in both, I move it to the finally block.

Collapse
 
functionalstoic profile image
JasonSooter

Nice tip!

Collapse
 
thejohnstew profile image
John Stewart

Such a well written article and I learned something new!

Collapse
 
annarankin profile image
Anna Rankin

Daww thank you! 😁 Love that gif

Collapse
 
kurisutofu profile image
kurisutofu

When I saw the code for try-catch-finally, my first thought was that you should not have a "return" in the try and catch if you are using finally.
My understanding was that finally has the "final say".

I find interesting the way promises handle that. Which I think makes sense too ... each action "block" would basically be a local try-catch-finally except the "catches" are grouped at the end for handling.

Collapse
 
annarankin profile image
Anna Rankin

Sorry I missed your comment - I agree, the multiple returns with the catch block are pretty wacky. The way Promises work makes sense looking at it now, but my brain totally rebelled on first look 😵Thanks kuristofu!

Collapse
 
annarankin profile image
Anna Rankin

It's a neat tool - and it's fun what you can learn when you try weird things lol. One thing I will note is that you might run into difficulty using Promise.finally in applications that are tested with Jest.

Collapse
 
rhymes profile image
rhymes

Thanks Anna, it's quite different on how finally works in contrast to other general purpose languages. Nice to know :-) Hopefully 99% of the time people will use finally just to cleanup...

Collapse
 
annarankin profile image
Anna Rankin

Thank you! And agreed 🙏

Side note, the history of decisions that go into defining how a language should work are fascinating 😵

Collapse
 
rhymes profile image
rhymes

It definitely is!

Collapse
 
aelafnstein profile image
Aelaf

Thank you!!! Promise I will use promise!!!

Collapse
 
annarankin profile image
Anna Rankin

I'm sorry I missed your comment! Glad you liked it 😊

Collapse
 
alifarag_90 profile image
AliFarag

Good article and great effort.

Collapse
 
annarankin profile image
Anna Rankin

Thank you Ali! 🙌

Collapse
 
sagar profile image
Sagar • Edited

Finally, Anna wonderful article.

Collapse
 
annarankin profile image
Anna Rankin

Thank you Sagar! 🙌

Collapse
 
diek profile image
diek

I am using finally in try/catch almost forever, but Ty your post I am aware of the the finally return feature. Good post!

Collapse
 
cdsaenz profile image
Charly S.

Beautiful article Thanks a lot.

Collapse
 
annarankin profile image
Anna Rankin

Thank you Charly! 😊

Collapse
 
revel109 profile image
Abu Yusuf

I would like work with my error.response/error.response.status in catch.
How to do that?

Collapse
 
antonitor profile image
Toni Torres

You'll have to throw new Error yourself in your try block, something like:
if (!response.ok) throw new Error("Ups!")