Explain the callback Promise.finally in JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

The promise.finally() calls the callback function whether a promise has been fulfilled or rejected. This helps us to do something once the promise has been rejected or resolved.

Following is the code for Promise.finally() in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 20px;
      font-weight: 500;
<h1>Promise.finally in JavaScript</h1>
<div style="color: green;" class="result"></div>
<button class="btn">CLICK HERE</button>
Click on the above button to know when the promise finishes
   let btnEle = document.querySelector(".btn");
   let resEle = document.querySelector(".result");
   const yearRegex = /(?\d{4})/g;
   btnEle.addEventListener("click", () => {
      .then((Response) => Response.json())
      .then((res) => res)
      .catch((err) => alert("error occured"))
      .finally(() => (resEle.innerHTML = "Promise is finished"));


The above code will produce the following output −

On clicking the ‘CLICK HERE’ button and waiting some seconds −

Published on 15-Jul-2020 13:05:01