Promise.prototype.finally
Runs an action after a promise — on success and on failure.
Available since: ES2018
Syntax
promise.finally(onFinally)Parameters
| Parameter | Type | Description |
|---|---|---|
| onFinally | Function | A 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 doneNotes
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
- Promise.prototype.then — Says what to do with a promise's result.
- Promise.prototype.catch — Handles a promise's error.
- try...catch — Catches errors so the program does not stop.
In the tutorial
- Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.