.finally()
Anonymous contributor
Anonymous contributor3071 total contributions
Anonymous contributor
Published Jul 27, 2021Updated Jun 28, 2023
Contribute to Docs
Returns a new Promise
object after the previous promise in the chain has been resolved or rejected. This last part of the chain will execute no matter what.
Syntax
myPromiseObject.then(fulfilledPromiseCallback).finally(finalPromiseCallback);
.finally()
can be used with or without .then()
and/or .catch()
.
Example
const myPromise = new Promise((resolve, reject) => {if (2 + 2 === 4) {resolve('Fulfilled');} else {reject('Rejected');}});myPromise.then((result) => {console.log(result);}).catch((err) => {console.log(`${err.status} - ${err.errorType}: ${err.message}`);}).finally(() => console.log('Operations have ended.'));/*Output:FulfilledOperations have ended.*/
Codebyte Example
In the following example, the callback function passed to finally()
is executed when the promise either resolves or rejects.
All contributors
- Anonymous contributorAnonymous contributor3071 total contributions
- BrandonDusch580 total contributions
- karel.de.smetoutlook.com7 total contributions
- christian.dinh2476 total contributions
- Anonymous contributor
- BrandonDusch
- karel.de.smetoutlook.com
- christian.dinh
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.