Skip to content

Instantly share code, notes, and snippets.

@Raynos Raynos/promise.js
Created Mar 20, 2013

Embed
What would you like to do?
//check if this call is a get.
function isGet(val) { return undefined === val }
//check if this call is a set, else, it's a listen
function isSet(val) { return 'function' !== typeof val }
function promise() {
var value, listeners = []
return function (listener) {
if (isGet(listener)) {
return value
} else if (isSet(listener)) {
if (value !== undefined) {
throw new Error("promise value already delivered")
} else {
value = listener
listeners.forEach(function (l) { l(value) })
}
} else {
listeners.push(listener)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.