Skip to content

Instantly share code, notes, and snippets.

@datfaf
Forked from fernandezpablo85/node-curry.js
Created September 5, 2011 10:13
Show Gist options
  • Save datfaf/1194629 to your computer and use it in GitHub Desktop.
Save datfaf/1194629 to your computer and use it in GitHub Desktop.
Explanation of the curry function for node.js
function curriedReadFile(path) {
var _done, _error, _result;
var callback = function(error, result) {
_done = true,
_error = error,
_result = result;
};
fs.readFile(path, function(e, r) {
callback(e, r);
});
// Here '_' is the function we pass to curriedReadFile
// Where we actually do the IO stuff
return function(_) {
// If fs.readFile returned (_done is set), we just execute our IO code with the results
if (_done) {
_(_error, _result);
// If it still hasn't return, our function that does IO stuff ('_')
// now becomes the callback (see fs.readFile body)
} else {
callback = _;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment