Lately, I've been experimenting more with the async/await keywords in JavaScript. I noticed that I sometimes struggle to reconcile the strategies I...
For further actions, you may consider blocking this person and/or reporting abuse
Good catch!
😜heeheehee
You gave it the good old college try with that one!
Great write-up! I've only ever found a single good use for
finally
. You could do performance checks usingconsole.time
.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.Nice tip!
Such a well written article and I learned something new!
Daww thank you! 😁 Love that gif
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.
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!
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.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...
Thank you! And agreed 🙏
Side note, the history of decisions that go into defining how a language should work are fascinating 😵
It definitely is!
Thank you!!! Promise I will use promise!!!
I'm sorry I missed your comment! Glad you liked it 😊
Good article and great effort.
Thank you Ali! 🙌
Finally, Anna wonderful article.
Thank you Sagar! 🙌
I am using finally in try/catch almost forever, but Ty your post I am aware of the the finally return feature. Good post!
Beautiful article Thanks a lot.
Thank you Charly! 😊
I would like work with my error.response/error.response.status in catch.
How to do that?
You'll have to throw new Error yourself in your try block, something like:
if (!response.ok) throw new Error("Ups!")