Created
May 4, 2011 15:32
-
-
Save fernandezpablo85/955421 to your computer and use it in GitHub Desktop.
Explanation of the curry function for node.js
This file contains 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 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
Is there a reason for wrapping
callback(e, r)
?Ie, why not simply: