Created
March 27, 2012 02:51
-
-
Save ishiduca/2212063 to your computer and use it in GitHub Desktop.
parseJSONFile
This file contains hidden or 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
var fs = require('fs'); | |
module.exports = function parseJSONFile (path, cb) { | |
if (typeof cb !== 'function') throw('2nd argument must be "Function"'); | |
try { | |
fs.readFile(path, 'utf-8', function (err, data) { | |
if (err) throw err; | |
cb(JSON.parse(data)); | |
}); | |
} catch (e) { throw e; } | |
}; |
Author
ishiduca
commented
Mar 27, 2012
var parser = require('./parseJSON');
parser('./path/to/json', function (err, data) {
console.log(err || data);
});
var parser = require('./parseJSON');
try {
parser('path/to/json', function (data) {
console.log(data);
});
} catch (e) { console.error(e); }
try {
require('./parseJSON')( 'path/to/json', function (data) {
console.log(data);
});
} catch (e) { console.log(e); }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment