Search⌘ K

Promise Chains with the finally() Method

Explore how to use the finally() method in JavaScript promise chains to manage the state and value of promises. Understand how it passes fulfillment or rejection states, handles errors within settlement handlers, and supports centralized UI updates in asynchronous code.

Using finally() in promise chains

The finally() method behaves differently than both then() and catch() in that it copies the state and value of the previous promise into its returned promise. That means if the original promise is fulfilled with a value, then finally() returns a promise that is fulfilled with the same value. For example, we have the following:

Javascript (babel-node)
const promise = Promise.resolve(11);
promise.finally(() => {
console.log("Finally called.");
}).then(value => {
console.log(value); // 11
});

Here, the settlement handler can’t receive the fulfilled value from promise, so that value is copied to a new promise that is returned from the method call. The new promise is fulfilled with the value 11 ...