Skip to content

Instantly share code, notes, and snippets.

@boostup
Forked from wuchengwei/readJsonFileAsync.js
Created March 30, 2018 08:13
Show Gist options
  • Save boostup/93c36b9b03d13bb94e69bca3383ecf4a to your computer and use it in GitHub Desktop.
Save boostup/93c36b9b03d13bb94e69bca3383ecf4a to your computer and use it in GitHub Desktop.
NodeJS - Read in a json file asynchronously
/*
* @param {String} filepath
* @param {function} callback function(err, data){}
*/
function readJsonFileAsync(filepath, callback) {
var fs = require('fs');
fs.readFile(filepath, 'utf-8', function(err, data) {
if (err) { callback(err, null); }
else {
result = JSON.parse(data);
if (result) {
callback(null, result);
} else {
callback('parse error', null);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment