-
-
Save boostup/93c36b9b03d13bb94e69bca3383ecf4a to your computer and use it in GitHub Desktop.
NodeJS - Read in a json file asynchronously
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
/* | |
* @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