I have a function getHighScores which I want to go into a JSON file and get score objects for a given game, sort them, and return the top amount of them.
This is my (broken) code:
function getHighScores(game, amount) {
    var highScores = null;
    fs.readFile('scores.json', 'utf8', function (error, data) {
        var scoresObj = JSON.parse(data);
        var gameScores = scoresObj.games[game];
        sortedScores = gameScores.sort(compareScoreObjects);
        highScores = sortedScores.slice(0, amount);
    });
    return highScores;
}
console.log(getHighScores('snake', 10));
This is logging null, because highScores cannot be accessed within the scope of the callback function for fs.readFile.
Is there any way to do this while maintaining the logic of my code - where I can simply have getHighScores return the data? If not, how should I be thinking about this problem?
 
    