This content originally appeared on DEV Community and was authored by Heru Hartanto
When you have a promise, sometimes you want to ensure that some code always runs, regardless of the outcome of the promise. For example, you may want to hide a loading spinner or close a file connection whether the operation was successful or not. Before finally(), developers had to duplicate code in both the .then() and .catch() blocks. finally() eliminates that redundancy.
fetchData()
.then((data) => {
console.log('Data fetched successfully', data);
})
.catch((error) => {
console.error('Error fetching data', error);
})
.finally(() => {
hideLoadingSpinner(); // Runs no matter the result
});
This content originally appeared on DEV Community and was authored by Heru Hartanto
Heru Hartanto | Sciencx (2024-08-31T11:33:23+00:00) Promise.finally(): Your Secret Weapon for Smoother Promise. Retrieved from https://www.scien.cx/2024/08/31/promise-finally-your-secret-weapon-for-smoother-promise/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.