What is promise.prototype.finally?
The promise.prototype.finally package provides a polyfill for the `finally` method on the Promise prototype. This method allows for a callback to be executed when the promise is settled, regardless of its outcome (fulfilled or rejected). It is useful for running cleanup code or finalizing operations without caring about the promise's resolution.
What are promise.prototype.finally's main functionalities?
Adding finally support to Promises
This feature allows for the addition of the `finally` method to the Promise prototype, enabling cleanup or final operations to be run regardless of whether the promise was fulfilled or rejected.
require('promise.prototype.finally').shim();
new Promise((resolve, reject) => {
// Asynchronous operation
}).finally(() => {
// Cleanup or final code to run regardless of promise outcome
});
Other packages similar to promise.prototype.finally
bluebird
Bluebird is a comprehensive promise library that includes a `finally` method among its wide array of features. Compared to promise.prototype.finally, Bluebird offers a much broader set of promise-related utilities, optimizations, and conveniences.
q
Q is another popular promise library that supports a `finally` method. It provides a rich set of promise manipulation and control flow utilities. While Q's `finally` method serves a similar purpose to that of promise.prototype.finally, Q is designed as a complete promise library rather than a polyfill.
Promise.prototype.finally
A polyfill for Promise.prototype.finally
. See docs on what finally is on your favourite pre-ES6 promise library.
Warning: This micro-library doesn't force you to use any particular Promise implementation by using whatever Promise
has been defined as globally. This is so that you may use any ES6 standard Promise compliant library - or, of course, native ES6 Promises.
If you're running the code on a browser or node version that doesn't include native promises you will need to include a polyfill. The following polyfills are tested as part of this module's test suite:-
Installation
npm install promise.prototype.finally --save
Examples
require('es6-promise').polyfill();
require('promise.prototype.finally');
Promise.resolve(6)
.finally(function() {
console.log('this will always be called');
});
Promise.reject(6)
.finally(function() {
console.log('this will always be called');
});
Credits and collaboration
The lead developer of Promise.prototype.finally is Matt Andrews at FT with unwitting help and support from Stefan Penner. All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request.