Last active
March 23, 2017 04:22
-
-
Save hosseinzoda/9167c6e000165522c7d283c4a5e37319 to your computer and use it in GitHub Desktop.
Promise.prototype.finally implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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