04.iii Promise.finally()
Finally method - a callback to be invoked when a promise is settled (either fulfilled, or rejected):
promise
    .then(result => {···})
    .catch(error => {···})
    .finally(() => {···});Features:
A
finally’s callback is always executedA
finallycallback will not receive any argumentPreserve Promise state
Promise.resolve(2).finally(() => {})will be resolved with 2.Promise.reject(3).finally(() => {})will be rejected with 3.
A
throw(or returning a rejected promise) in thefinallycallback will reject the new promise with that rejection reason.
promise.finally(() => {
    «statements»
});
// Is equal to:
promise.then(
    result => {
        «statements»
        return result;
    },
    error => {
        «statements»
        throw error;
    }
);The essential use case here is cleanup - I want to hide the "loading" spinner on my AJAX request, or I want to close any file handles I’ve opened, or I want to log that an operation has completed regardless of whether it succeeded or not.
Last updated
Was this helpful?