Basic node-style callback error helper for iced-coffee-script
Originally described by @maxtaco as the "Error Short-Circuiter" pattern:
http://maxtaco.github.io/programming/2014/09/18/handling-errors-in-iced-coffee-script/
- Creates a function that runs one function when a callback receives an error as the first argument, and runs a different function when a callback receives a null first argument (the nodejs callback pattern)
- Flattens nested callbacks
- Restores sanity
npm install --save errifyEscape automatically to callback when fs.readFile(...) fails:
nextorigin/express-rendertype/.../src/fancy.coffee#L22
For readability, I prefer using ideally as the errify keyword. In the spirit of coffee-script it reads like English:
{readDir, readFile} = fs
tryToReadTheFirstFileFrom = (directory, callback) ->
ideally = errify callback
await readDir directory, ideally defer files
[file] = files
await readFile file, ideally defer contents
callback null, {files, contents}
ideally = console.error.bind console
folder = "/path/to/a/list/of/files"
await tryToReadTheFirstFileFrom folder, ideally defer {files, contents}
console.log contentsMIT