<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luca</title>
    <description>The latest articles on DEV Community by Luca (@lucamattiazzi).</description>
    <link>https://dev.to/lucamattiazzi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3982%2F16397386.jpeg</url>
      <title>DEV Community: Luca</title>
      <link>https://dev.to/lucamattiazzi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucamattiazzi"/>
    <language>en</language>
    <item>
      <title>The two meanings of `finally` in Javascript</title>
      <dc:creator>Luca</dc:creator>
      <pubDate>Wed, 11 Nov 2020 08:09:20 +0000</pubDate>
      <link>https://dev.to/lucamattiazzi/the-two-meanings-of-finally-in-javascript-22bi</link>
      <guid>https://dev.to/lucamattiazzi/the-two-meanings-of-finally-in-javascript-22bi</guid>
      <description>&lt;p&gt;As many, if not all, JS developers know, there are two different syntaxes that handle promises, the &lt;code&gt;then&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;/&lt;code&gt;finally&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;runPromiseThen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fnThatReturnsPromise&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promiseResult&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doSomethingSyncWithResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promiseResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;possiblyLogAndHandleErrorSomehow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;doSomethingLikeNotifyThatThisIsOver&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;runPromiseAwait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promiseResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fnThatReturnsPromise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doSomethingSyncWithResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promiseResult&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;possiblyLogAndHandleErrorSomehow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;doSomethingLikeNotifyThatThisIsOver&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While they might look like the same, and they mostly behave in the same way, the two functions will return totally different values when called.&lt;/p&gt;

&lt;p&gt;In particular, &lt;code&gt;runPromiseThen&lt;/code&gt; will return:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;result&lt;/code&gt; when the &lt;code&gt;then&lt;/code&gt; path is taken, as no error have been thrown during the execution.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;null&lt;/code&gt; if an error is thrown, since the &lt;code&gt;catch&lt;/code&gt; path is taken.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both cases though, the function &lt;code&gt;doSomethingLikeNotifyThatThisIsOver&lt;/code&gt; is called since that's what &lt;code&gt;finally&lt;/code&gt; does: it defines a block that is always run no matter what path is taken. Not only that, but the block needs to be run until the end before the promise is resolved. So remember not to put a long sync function in a &lt;code&gt;finally&lt;/code&gt; block unless you want to slow down your promise.&lt;/p&gt;

&lt;p&gt;And the &lt;code&gt;finally&lt;/code&gt; in &lt;code&gt;runPromiseAwait&lt;/code&gt; does the same: no matter what path the execution takes, be it the &lt;code&gt;try&lt;/code&gt; or the &lt;code&gt;catch&lt;/code&gt;, whatever code is written in its blocks is executed.&lt;/p&gt;

&lt;p&gt;But there is a &lt;code&gt;catch&lt;/code&gt;!&lt;br&gt;
While the &lt;code&gt;finally&lt;/code&gt; in a &lt;code&gt;then&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;/&lt;code&gt;finally&lt;/code&gt; will not change what the function returns, the &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; will: whatever the &lt;code&gt;finally&lt;/code&gt; block returns, is what the whole function will return; no matter what path the execution takes, no matter what &lt;code&gt;fnThatReturnsPromise&lt;/code&gt; returns, &lt;code&gt;runPromiseAwait&lt;/code&gt; will always return &lt;code&gt;null&lt;/code&gt;, since that's what the &lt;code&gt;finally&lt;/code&gt; block returns.&lt;/p&gt;

&lt;p&gt;That's it.&lt;br&gt;
This is my first post on dev.to, I'm trying to force myself into writing more.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promise</category>
      <category>finally</category>
    </item>
  </channel>
</rss>
