Promise.prototype.finally
Attaches a callback that is invoked when the promise is settled (either fulfilled or rejected), and returns a new Promise
Syntax
promise.finally(onFinally)Parameters
| Parameter | Type | Description |
|---|---|---|
| onFinally | () => void | Callback to execute when the promise is settled |
Return Value
A new Promise
Examples
let loading = true;
fetch('/api/data')
.then(r => r.json())
.catch(err => console.error(err))
.finally(() => { loading = false; });function fetchWithSpinner(url: string) {
showSpinner();
return fetch(url)
.then(r => r.json())
.finally(() => hideSpinner());
}Promise.resolve('done')
.finally(() => console.log('cleanup'))
.then(val => console.log(val));
// 'cleanup'
// 'done'Understanding Promise.prototype.finally
The Promise.prototype.finally method in JavaScript attaches a callback that is invoked when the promise is settled (either fulfilled or rejected), and returns a new Promise. It belongs to the Promise object and is one of the most widely used methods for working with promise values in modern JavaScript and TypeScript applications.
The method signature is promise.finally(onFinally). It accepts 1 parameter: onFinally. When called, it returns a new promise. Understanding when and how to use finally() helps you write more expressive, readable code.
Common use cases for Promise.prototype.finally include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like promise-then, promise-catch, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Promise.prototype.finally is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Promise.prototype.thenAttaches callbacks for the resolution and/or rejection of the Promise, and returns a new Promise
Promise.prototype.catchAttaches a rejection handler callback to the promise and returns a new Promise resolving to the return value of the callback if called, or the original fulfilled value if the promise is resolved
More Promise Methods
Other methods in the Promise object
Related Tools
More Promise Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.