Promises

Promise.prototype.finally()

ECMAScript API used in Node.js: runs cleanup after fulfillment or rejection. The original outcome normally passes through, but throwing or returning a rejected promise from cleanup changes the outcome. Prefer try/finally around awaited resource ownership when that reads more clearly. Tested on Node.js 22.16.0. Example output (one line per entry): ["report false", "cleanup", "failed"].

Formal Syntax & Structure

nodejs syntaxnodejs
promise.finally(onFinally)

Parameters & Attributes

NameTypeDescription
onFinallyFunctionCleanup callback; no result argument.

Return Value

Promise

A new promise that waits for cleanup.

Working Example

Example Snippetnodejs
let active = true;const result = await Promise.resolve('report').finally(() => { active = false; });console.log(result, active);try {    await Promise.reject(new Error('failed')).finally(() => console.log('cleanup'));}catch (e) {    console.log(e.message);}
Node.js Reference