Skip to content

Instantly share code, notes, and snippets.

@remydagostino
Created June 27, 2016 14:54
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 remydagostino/ca1ef51eefff2b79bdd0036fc1e6a576 to your computer and use it in GitHub Desktop.
Save remydagostino/ca1ef51eefff2b79bdd0036fc1e6a576 to your computer and use it in GitHub Desktop.
Minimal continuation passing construct
function thenable(action) {
var self = {
fork: function(succ, err) {
action(succ || function() {}, err || function() {});
},
then: function(fn) {
return thenable(function(resolve, reject) {
self.fork(
function(data) {
var step = fn(data);
if (step && step.then) {
step.fork(resolve, reject);
} else {
resolve(step);
}
},
function(e) {
reject(e);
}
);
});
}
};
return self;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment