With consideration of the following code-block, how would one go about having loadConfig() return the JSON config object?
function loadConfig(){
fs.readFile('./config.json', 'utf8', function (err, data){
if (err) throw err;
var config = JSON.parse(data);
});
return config;
};
The returned config is undefined as the variable config is outside the scope of the loadConfig() function, yet if the the return statement is located inside the readFile anonymous function, it doesn't fall through to loadConfig(), and seemingly only breaks the nested anonymous function.
Another attempt has been made to resolve this by saving the anonymous function in a variable which is then returned by the main function loadConfig, but to no avail.
function loadConfig(){
var config = fs.readFile('./config.json', 'utf8', function (err, data){
if (err) throw err;
var config = JSON.parse(data);
return config;
});
return config;
};
The question stands; given situation sketched above, how would one make loadConfig() return the config JSON object?