cryptostan101
4/9/2018 - 8:15 PM

Read json file asynchronously with nodejs core lib

Read json file asynchronously with nodejs core lib

// define variables
let fs = require('fs');
let callback = callback || function(){};

// call readJsonFileAsync


// function
function readJsonFileAsync(filepath, callback) {

    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);
            }
        }
    });
}