Last active
October 1, 2019 01:12
-
-
Save joshuakemmerling/c069bf9e4237043fddcc to your computer and use it in GitHub Desktop.
Promise JS
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
new Promise(function (reject) { | |
}).then(function (reject) { | |
reject('this is an error'); | |
}).then(function () { | |
console.log('never run'); | |
}).catch(function (err) { | |
console.log('err', err); | |
}).finally(function () { | |
}); |
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
function Promise (func) { | |
this.hasError = false; | |
this.error = null; | |
var that = this, | |
reject = function (err) { | |
that.hasError = true; | |
that.error = err; | |
}; | |
(func || function(){})(reject); | |
} | |
Promise.prototype.then = function (func) { | |
var that = this, | |
reject = function (err) { | |
that.hasError = true; | |
that.error = err; | |
}; | |
if (!this.hasError) | |
(func || function(){})(reject); | |
return this; | |
}; | |
Promise.prototype.catch = function (func) { | |
if (this.hasError) | |
(func || function(){})(this.error); | |
return this; | |
}; | |
Promise.prototype.finally = function (func) { | |
(func || function(){})(); | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment