Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Promise.prototype.finally — cleanup after a promise settles

Promise.prototype.finally runs a callback after a promise fulfills or rejects. It provides shared cleanup that normally preserves the original outcome, although a cleanup failure can replace that outcome.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

promise.finally(onFinally)

Promise.prototype.finally registers onFinally, called asynchronously after the promise fulfills or rejects. The callback receives no arguments: neither the value nor the rejection reason. For standard promises, the method returns a new promise without changing the original.

An ordinary value returned by onFinally is ignored. If the callback completes successfully, the chain preserves the original value or rejection reason. If it returns a promise or thenable, the chain waits for it to settle. A thrown exception or rejection replaces the original outcome with a new rejection; remaining pending forever blocks the subsequent chain.

This is not equivalent to then(onFinally, onFinally): those callbacks receive an argument, and their result can replace the previous value. finally() does not itself handle an error — use catch() on the returned chain. A non-function argument is ignored by standard promise handling.

The method has been part of ECMAScript since ES2018, not Web APIs. The language specification does not establish support in a particular browser; Node 24 in a repository does not prove it either. In the example, "ignored" does not replace 42, and the second cleanup does not remove the rejection.

Examples

Promise.resolve(42)
  .finally(() => {
    console.log("cleanup");
    return "ignored";
  })
  .then((value) => {
    console.log(value);
    return Promise.reject("failure");
  })
  .finally(() => {
    console.log("cleanup after rejection");
  })
  .catch((reason) => console.log(reason));

Result

cleanup
42
cleanup after rejection
failure

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources