DEV Community

Heru Hartanto
Heru Hartanto

Posted on

1

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
  });
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more