Skip to content

Instantly share code, notes, and snippets.

@JBreit
Forked from fernandezpablo85/node-curry.js
Created April 1, 2016 17:22
Show Gist options
  • Save JBreit/219db866b28e64390b4725f713c539d1 to your computer and use it in GitHub Desktop.
Save JBreit/219db866b28e64390b4725f713c539d1 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