Skip to content

Instantly share code, notes, and snippets.

@thoov
Created August 23, 2013 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoov/6315496 to your computer and use it in GitHub Desktop.
Save thoov/6315496 to your computer and use it in GitHub Desktop.
function Promise(promise) {
if (promise instanceof Promise) {
return promise;
} else {
// this is a new promise chain
this.callbacks = [];
}
}
Promise.prototype.then = function(callback_ok, callback_error) {
this.callbacks.push({
ok: callback_ok,
error: callback_error
});
return this;
};
Promise.prototype.resolve = function() {
var callback = this.callbacks.shift();
if (callback && callback.ok) {
callback.ok.apply(this, arguments);
}
};
Promise.prototype.reject = function() {
var callback = this.callbacks.shift();
if (callback && callback.error) {
callback.error.apply(this, arguments);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment