Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created March 20, 2013 20:51
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 Raynos/5208304 to your computer and use it in GitHub Desktop.
Save Raynos/5208304 to your computer and use it in GitHub Desktop.
//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