
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain the callback Promise.finally in JavaScript
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 −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Promise.finally in JavaScript</h1> <div style="color: green;" class="result"></div> <button class="btn">CLICK HERE</button> <h3> Click on the above button to know when the promise finishes </h3> <script> let btnEle = document.querySelector(".btn"); let resEle = document.querySelector(".result"); const yearRegex = /(?\d{4})/g; btnEle.addEventListener("click", () => { fetch("https://jsonplaceholder.typicode.com/users") .then((Response) => Response.json()) .then((res) => res) .catch((err) => alert("error occured")) .finally(() => (resEle.innerHTML = "Promise is finished")); }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button and waiting some seconds −
- Related Articles
- Explain the finally Statement in JavaScript with examples.
- How to call promise inside another promise in JavaScript?
- Explain the role of callback function in AJAX
- What is Promise Chaining in JavaScript?
- What are callback functions in JavaScript?
- Explain Try/Catch/Finally block in PowerShell
- what is the significance of finally in javascript?
- Passing parameters to callback functions JavaScript
- Explain try, except and finally statements in Python.
- Passing a function as a callback in JavaScript
- How to create a global Promise Rejection Handler in JavaScript?
- Handling Promise rejection with a catch while using await in JavaScript
- Callback function in C
- How do we use try...catch...finally statement in JavaScript?
- What is difference between Microtask Queue and Callback Queue in asynchronous JavaScript?

Advertisements