Socket
Socket
Sign inDemoInstall

promise.prototype.finally

Package Overview
Dependencies
65
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    promise.prototype.finally

ES Proposal spec-compliant shim for Promise.prototype.finally


Version published
Weekly downloads
2M
decreased by-0.3%
Maintainers
2
Install size
3.14 MB
Created
Weekly downloads
 

Package description

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

Readme

Source

promise.prototype.finally Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

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);
});

Tests

Simply clone the repo, npm install, and run npm test

Thanks

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

Keywords

FAQs

Last updated on 05 Feb 2024

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc