What is the difference between then and finally in a promise?
<p>I see the docs for Bluebird's <a href="http://bluebirdjs.com/docs/api/finally.html" rel="noreferrer"><code>finally</code></a> but I still don't quite understand the difference vs. <code>then</code>.</p>

<p>To be clear: I know exactly why <code>then</code> gets called after a <code>catch</code>. I <em>want</em> it to be called after a catch. That's the intent. My question is: If I <em>want</em> code to always be executed regardless of the promise state, what's the difference between <code>then</code> vs <code>finally</code> for that?</p>

<p>I built this test:</p>

<pre><code>var Promise = require("bluebird");

function test1 () {
    console.log("RESOLVE + THEN + CATCH + THEN");
    return new Promise((resolve, reject) =&gt; resolve())
       .then(() =&gt; console.log("then"))
       .catch(err =&gt; console.log("error:", err.message))
       .then(() =&gt; console.log("end"));
}

function test2 () {
    console.log("REJECT + THEN + CATCH + THEN");
    return new Promise((resolve, reject) =&gt; reject(new Error("rejected")))
       .then(() =&gt; console.log("then"))
       .catch(err =&gt; console.log("error:", err.message))
       .then(() =&gt; console.log("end"));
}

function test3 () {
    console.log("RESOLVE + THEN + CATCH + FINALLY");
    return new Promise((resolve, reject) =&gt; resolve())
       .then(() =&gt; console.log("then"))
       .catch(err =&gt; console.log("error:", err.message))
       .finally(() =&gt; console.log("end"));
}

function test4 () {
    console.log("REJECT + THEN + CATCH + FINALLY");
    return new Promise((resolve, reject) =&gt; reject(new Error("rejected")))
       .then(() =&gt; console.log("then"))
       .catch(err =&gt; console.log("error:", err.message))
       .finally(() =&gt; console.log("end"));
}

// run tests "sequentially" so console output doesn't get blended
setTimeout(test1, 500);
setTimeout(test2, 1000);
setTimeout(test3, 1500);
setTimeout(test4, 2000);
</code></pre>

<p>This tests four cases:</p>

<ol>
<li><code>.then(...).catch(...).then(...)</code> with a resolved promise.</li>
<li><code>.then(...).catch(...).then(...)</code> with a rejected promise.</li>
<li><code>.then(...).catch(...).finally(...)</code> with a resolved promise.</li>
<li><code>.then(...).catch(...).finally(...)</code> with a rejected promise.</li>
</ol>

<p>The results I see are cases 1+2 behaves identically to 3+4: The last bit (<code>then</code> or <code>finally</code> depending on test) executes no matter what happens before it, as intended. The output of that program is:</p>

<pre><code>RESOLVE + THEN + CATCH + THEN
then
end
REJECT + THEN + CATCH + THEN
error: rejected
end
RESOLVE + THEN + CATCH + FINALLY
then
end
REJECT + THEN + CATCH + FINALLY
error: rejected
end
</code></pre>

<p>Now, the reason I'm asking is because I saw a <a href="https://stackoverflow.com/questions/41108293/allowing-only-one-async-operation-at-a-time?noredirect=1#comment69462006_41108293">comment on this other question I asked</a>:</p>

<blockquote>
  <p>Not sure if your promises support it, but you should change the last <code>.then</code> to <code>.finally</code> so that the <code>busy</code> always gets cleared.</p>
</blockquote>

<p>From my very limited knowledge of <code>then</code>, and the tests above, it seems like <code>then</code> is sufficient. But after that comment I'm questioning myself and the safety of using <code>then</code> to execute "finally" code.</p>

<p>So my question is: What's the difference between <code>then</code> and <code>finally</code>? They <em>look</em> like they behave the same, but when would I need to use <code>finally</code> instead of <code>then</code>?</p>