Skip to content

Instantly share code, notes, and snippets.

@hosseinzoda
Last active March 23, 2017 04:22
Show Gist options
  • Save hosseinzoda/9167c6e000165522c7d283c4a5e37319 to your computer and use it in GitHub Desktop.
Save hosseinzoda/9167c6e000165522c7d283c4a5e37319 to your computer and use it in GitHub Desktop.
Promise.prototype.finally implementation
/**
* I think this is correct implementation of promise.finally,
* Following what synchronized try/catch/finally does
*/
if(!Promise.prototype.finally) {
Promise.prototype.finally = function(f) {
var ret, thr, hasthr = false;
return this.then(function(a){ ret = a; return f(); })
.then(function(){ return ret; })
.catch(function(a) { hasthr = true; thr = a; return f(); })
.then(function(a){ if(hasthr) throw thr; else return a; });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment