Created
June 27, 2016 14:54
-
-
Save remydagostino/ca1ef51eefff2b79bdd0036fc1e6a576 to your computer and use it in GitHub Desktop.
Minimal continuation passing construct
This file contains hidden or 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 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