Skip to content

Instantly share code, notes, and snippets.

@julienw
Last active August 29, 2015 14:00
Show Gist options
  • Save julienw/11362924 to your computer and use it in GitHub Desktop.
Save julienw/11362924 to your computer and use it in GitHub Desktop.
Promise.prototype.finally = function(callback) {
return this.then(
function(arg) {
function returnArg() { return arg; }
return Promise.resolve(arg).then(callback).then(returnArg, returnArg);
},
function(arg) {
function throwArg() { throw arg; }
return Promise.resolve(arg).then(callback).then(throwArg, throwArg);
}
);
};
Promise.prototype.finally = function(callback) {
return this.then(
function(arg) {
function returnArg() { return arg; }
return Promise.resolve().then(callback).then(returnArg, returnArg);
},
function(arg) {
function throwArg() { throw arg; }
return Promise.resolve().then(callback).then(throwArg, throwArg);
}
);
};
@julienw
Copy link
Author

julienw commented Apr 28, 2014

Added a second possibility where we don't pass the arg to the finally callback, as I'm not sure it's useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment