Last active
December 15, 2015 13:58
-
-
Save Voronenko/5270921 to your computer and use it in GitHub Desktop.
Pure javascript simplified promis
http://jsfiddle.net/dpecos/HDbBa/e pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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