Skip to content

Instantly share code, notes, and snippets.

@Voronenko
Last active December 15, 2015 13:58
Show Gist options
  • Save Voronenko/5270921 to your computer and use it in GitHub Desktop.
Save Voronenko/5270921 to your computer and use it in GitHub Desktop.
Pure javascript simplified promis http://jsfiddle.net/dpecos/HDbBa/e pattern
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