Home > Web Front-end > JS Tutorial > body text

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

PHPz
Release: 2024-09-01 21:02:40
Original
615 people have browsed it

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. 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
  });
Copy after login

The above is the detailed content of Promise.finally(): Your Secret Weapon for Smoother Promise. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!