Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Promise.prototype.finally

Runs an action after a promise — on success and on failure.

Available since: ES2018

Syntax

promise.finally(onFinally)

Parameters

ParameterTypeDescription
onFinallyFunctionA function without arguments that runs in any case.

Return value

PromiseA promise with the same result or error as the original.

Examples

Hiding a loading indicator in any case:

console.log('loading...');
 
Promise.reject(new Error('Timeout'))
  .catch((error) => console.log('Error:', error.message))
  .finally(() => console.log('loading done'));

Output:

loading...
Error: Timeout
loading done

Notes

The finally() function receives neither the result nor the error and does not change them. The await equivalent is the finally block of try...catch.

See also

In the tutorial

  • Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.

Thanks for reading!

Donate to the site — we work hard for you.