Skip to content

Instantly share code, notes, and snippets.

@joshuakemmerling
Last active October 1, 2019 01:12
Show Gist options
  • Save joshuakemmerling/c069bf9e4237043fddcc to your computer and use it in GitHub Desktop.
Save joshuakemmerling/c069bf9e4237043fddcc to your computer and use it in GitHub Desktop.
Promise JS
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 () {
});
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