Created
August 10, 2020 21:53
-
-
Save mmarchini/18ac6a76f5fda70e3c35d88f0f36952f to your computer and use it in GitHub Desktop.
callback-readFile.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
// 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