Promise.finally(): Your Secret Weapon for Smoother Promise

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. Befo…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Promise.finally(): Your Secret Weapon for Smoother Promise." Heru Hartanto | Sciencx - Saturday August 31, 2024, https://www.scien.cx/2024/08/31/promise-finally-your-secret-weapon-for-smoother-promise/
HARVARD
Heru Hartanto | Sciencx Saturday August 31, 2024 » Promise.finally(): Your Secret Weapon for Smoother Promise., viewed ,<https://www.scien.cx/2024/08/31/promise-finally-your-secret-weapon-for-smoother-promise/>
VANCOUVER
Heru Hartanto | Sciencx - » Promise.finally(): Your Secret Weapon for Smoother Promise. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/31/promise-finally-your-secret-weapon-for-smoother-promise/
CHICAGO
" » Promise.finally(): Your Secret Weapon for Smoother Promise." Heru Hartanto | Sciencx - Accessed . https://www.scien.cx/2024/08/31/promise-finally-your-secret-weapon-for-smoother-promise/
IEEE
" » Promise.finally(): Your Secret Weapon for Smoother Promise." Heru Hartanto | Sciencx [Online]. Available: https://www.scien.cx/2024/08/31/promise-finally-your-secret-weapon-for-smoother-promise/. [Accessed: ]
rf:citation
» Promise.finally(): Your Secret Weapon for Smoother Promise | Heru Hartanto | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.