Skip to content

Instantly share code, notes, and snippets.

@cpetzold
Created December 19, 2012 22:02
Show Gist options
  • Save cpetzold/4340961 to your computer and use it in GitHub Desktop.
Save cpetzold/4340961 to your computer and use it in GitHub Desktop.
function promise() {
var finished = false,
result = null,
waiters = [];
return {
fulfil: function(res) {
finished = true;
result = res;
forEach(waiters, function(cb) {
cb(result);
});
},
get: function(callback) {
if (result) return callback(result);
waiters.push(callback);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment