JSFiddle - React, Tailwind, and code Playground
by mkuklis
JavaScript
function Promise(context) {
this.context = context || this;
this.success = [];
this.error = [];
}
/*
Promise.prototype.then = function (success, error) {
if (success) {
if (this.resolved) {
success.apply(this.context, this.resolved);
}
else {
this.success.push(success);
}
}
if (error) {
if (this.rejected) {
error.apply(this.context, this.rejected);
}
else {
this.error.push(error);
}
}
return this;
};
*/
Promise.prototype.resolve = function () {
//var callback;
this.resolved = arguments;
this.error = [];
while ((var callback = this.success.shift())) {
callback.apply(this.context, this.resolved);
}
};
Promise.prototype.reject = function () {
//var callback;
this.rejected = arguments;
this.success = [];
while ((var callback = this.error.shift())) {
callback.apply(this.context, this.rejected);
}
};
//var promise = new Promise(this);
/*
promise.then(function (arg) {
console.log('success ' + arg);
}, function (arg) {
console.log('error ' + arg);
});
//promise.reject(2);
promise.resolve(2);
*/