At this point I've used so many different promise libraries that I've lost track of what's supported natively by ES6 promises. Today I learned that “finally” is not yet part of the standard even though it's commonly implemented by third party promise libraries. In this post I will show how to simulate “finally” while we wait for the standard to hopefully implement it.
The typical use case for promise.finally is doing some sort of clean up work. One example that comes to mind is turning off loading indicators when dealing with ajax calls.
“Finally” is convenient since it will fire for both successful and rejected promises. This works well for loading indicators since we want to turn them off for both success and failure scenarios.
There is an active proposal to add finally to promises here.
While we wait for this to become reality, we have to either rely on shims, or create our own approximation of “finally”.
I think the easiest way to create the illusion of a “finally” is to add an extra “then” to your promise chain.
Here's an example of what I'm talking about:
Since the “then” comes after a “catch” we get a decent approximation of a “finally”.
The last then, or “finally” if you will, fires during both failure and success.