Skip to content

Instantly share code, notes, and snippets.

@mmarchini
Created August 10, 2020 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmarchini/18ac6a76f5fda70e3c35d88f0f36952f to your computer and use it in GitHub Desktop.
Save mmarchini/18ac6a76f5fda70e3c35d88f0f36952f to your computer and use it in GitHub Desktop.
callback-readFile.js
// Callback version
const { readFile } = require('fs');
function readJsonFile(file, cb) {
readFile(file, (err, data) => {
if (err) {
// If error while reading file, propagate the error via callback
return cb(err, null);
}
// Unexpected invalid JSON input, code will throw
cb(err, JSON.parse(data));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment