我正在尝试从异步函数访问返回的值,但我一直未定义。我从SO尝试了很多建议,但似乎无法满足我的需求。我希望能够访问logs数组中的值。
const jsonPath = '';
let logs = [];
function getLogs(folderPath, callback){
fs.readdir(folderPath, (err, files) => {
if(!err){
files.forEach(file => {
let response = fs.readFileSync(folderPath + '/' + file, 'utf8')
logs.push(JSON.parse(response).changelog);
})
}
else{
console.log('Error: ' + err);
}
callback(logs);
})
}
getChangelogs(jsonPath, function(result){
return logs; //I want to be able to access this logs array
})