Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
promise.prototype.finally
Advanced tools
ES Proposal spec-compliant shim for 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.
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
});
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 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.
ES Proposal spec-compliant shim for Promise.prototype.finally. Invoke its "shim" method to shim Promise.prototype.finally
if it is unavailable or noncompliant. Note: a global Promise
must already exist: the es6-shim is recommended.
This package implements the es-shim API interface. It works in an ES3-supported environment that has Promise
available globally, and complies with the proposed spec.
Most common usage:
var assert = require('assert');
var promiseFinally = require('promise.prototype.finally');
var resolved = Promise.resolve(42);
var rejected = Promise.reject(-1);
promiseFinally(resolved, function () {
assert.equal(arguments.length, 0);
return Promise.resolve(true);
}).then(function (x) {
assert.equal(x, 42);
});
promiseFinally(rejected, function () {
assert.equal(arguments.length, 0);
}).catch(function (e) {
assert.equal(e, -1);
});
promiseFinally(rejected, function () {
assert.equal(arguments.length, 0);
throw false;
}).catch(function (e) {
assert.equal(e, false);
});
promiseFinally.shim(); // will be a no-op if not needed
resolved.finally(function () {
assert.equal(arguments.length, 0);
return Promise.resolve(true);
}).then(function (x) {
assert.equal(x, 42);
});
rejected.finally(function () {
assert.equal(arguments.length, 0);
}).catch(function (e) {
assert.equal(e, -1);
});
rejected.finally(function () {
assert.equal(arguments.length, 0);
throw false;
}).catch(function (e) {
assert.equal(e, false);
});
Simply clone the repo, npm install
, and run npm test
Huge thanks go out to @matthew-andrews, who provided the npm package name for v2 of this module. v1 is both in the original repo and preserved in a branch
FAQs
ES Proposal spec-compliant shim for Promise.prototype.finally
The npm package promise.prototype.finally receives a total of 1,378,180 weekly downloads. As such, promise.prototype.finally popularity was classified as popular.
We found that promise.prototype.finally demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.