I am trying to reimplement promises library. From my understanding, then
listens to when a promises state changes and executes a success callback or failure callback depending on the results. From MDN documentation it seems like catch
has something to do with error resolution- I thought this was what then is for though. What is the difference between them?
Here is my current code:
//Not sure what this is for
var rejected = {}, resolved = {}, waiting = {};
var Promise = function (value, status) {
};
Promise.prototype.then = function (success, _failure) {
var context = this;
setInterval(function() {
if (context.status) {
success();
} else if (success === undefined) {
return;
} else {
_failure();
}
}, 100);
};
Promise.prototype.catch = function (failure) {
return failure;
};
var Deferred = function (promise) {
this.promise = promise || new Promise();
this.promise.status = undefined;
};
Deferred.prototype.resolve = function (data) {
this.promise.data = data;
this.promise.status = true;
};
Deferred.prototype.reject = function (error) {
this.promise.data = error;
this.promise.status = false;
};
var defer = function () {
return new Deferred();
};
module.exports.defer = defer;