A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-promise-finally-method/ below:

JavaScript Promise finally() Method - GeeksforGeeks

JavaScript Promise finally() Method

Last Updated : 23 Jul, 2025

The finally() method of the Promise object is used to return a callback when a Promise is settled (either fulfilled or rejected).

Syntax:
task.finally(onFinally() {
});
Parameters:

This method has a single parameter as mentioned above and described below:

Return Value:

It returns a Promise whose finally handler is set to the specified function.

Example: The below example demonstrates the finally() method:

JavaScript
// Define the Promise
let task = new Promise((resolve, reject) => {
    setTimeout(() => {

        // Reject the Promise
        reject("Promise has been rejected!");
    }, 2000);
});

task
    .then(
        (data) => {
            console.log(data);
        },

        // Handle any error
        (error) => {
            console.log("Error:", error);
        }
    )

    // Specify the code to be executed 
    // after the Promise is settled
    .finally(() => {
        console.log(
            "This is finally() block that is " +
            "executed after Promise is settled"
        );
    });

Output:

Error: Promise has been rejected!
This is finally() block that is executed after Promise is settled
Supported Browsers:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4