Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Forked from developit/*finally-polyfill.md
Created January 22, 2020 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gladchinda/3049dfb63dcb295b507a71fcb42d0f23 to your computer and use it in GitHub Desktop.
Save gladchinda/3049dfb63dcb295b507a71fcb42d0f23 to your computer and use it in GitHub Desktop.

finally-polyfill

A tiny 153-byte polyfill for Promise.prototype.finally.

Useful for browsers that support Promise but not the .finally() method.

Usage

npm install finally-polyfill

It's a polyfill, so you can import, require() or drop it in a script tag - all the same.

import 'finally-polyfill';
Promise.reject('oops').finally(() => { /* ... */ })
<script src="/path/to/finally-polyfill.min.js"></script>

License

MIT

Promise.prototype.finally = function(callback) {
if (typeof callback !== 'function') {
return this.then(callback, callback);
}
const P = this.constructor || Promise;
return this.then(
value => P.resolve(callback()).then(() => value),
err => P.resolve(callback()).then(() => { throw err; })
);
};
Promise.prototype.finally=function(n){if("function"!=typeof n)return this.then(n,n);var t=this.constructor||Promise;return this.then(function(r){return t.resolve(n()).then(function(){return r})},function(r){return t.resolve(n()).then(function(){throw r})})};
{
"version": "0.1.0",
"name": "finally-polyfill",
"main": "finally-polyfill.min.js",
"author": "Jason Miller <jason@developit.ca>",
"repo": "gist:d970bac1843",
"homepage": "https://gist.github.com/developit/d970bac18430943e4b3392b029a2a96c",
"scripts": { "prepack": "mv *finally-polyfill.md README.md", "postpack": "mv README.md *finally-polyfill.md" },
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment