Skip to content

Instantly share code, notes, and snippets.

@riston
Last active December 21, 2015 07:09
Show Gist options
  • Save riston/6269186 to your computer and use it in GitHub Desktop.
Save riston/6269186 to your computer and use it in GitHub Desktop.
Callback with mutable error message.
var fs = require('fs');
var errorHandler = function(err, result) {
return function(customMessage) {
if (err) return err;
else {
// Call some other sync function but with my message
console.log(customMessage);
handleResult(result);
}
}
};
fs.readFile('readme.md', 'utf-8', errorHandler()('hahahhah this does not work as this requires extra wrapper'));
fs.readFile('other.md', 'utf-8', errorHandler()('Actually the main point is to reuse code'));
@caitp
Copy link

caitp commented Aug 19, 2013

I think you want something more like

function fileReadHandler(message) {
  return function(err, data) {
    if(err) throw err
    else {
      console.log(message)
      handleData(data)
    }
  }
}

fs.readFile('file.foo', 'utf-8', fileReadHandler('custom message'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment