我有一个函数可以从文件中读取日期并将它们存储到一个数组中。它是通过 async/await 功能异步实现的。问题是它以错误的顺序执行:const util = require('util');const fs = require('fs');const path = require('path');const readFile = util.promisify(fs.readFile);const readDirectory = util.promisify(fs.readdir);// Retrieve logs from logs files and store them into an arrayconst getAuditLogsData = async () => { const logsFolderPath = path.join(__dirname, '../', 'logs'); const logData = []; try { const files = await readDirectory(logsFolderPath); // 1ST - OK console.log(files); files.forEach(async (file) => { const content = await readFile(logsFolderPath + '/' + file, 'utf-8'); const logList = JSON.parse(content); logList.forEach((log) => { // 4TH - NOT OK console.log(1) logData.push(log); }); }); // 2ND - NOT OK console.log(2); } catch (error) { console.log(error); } // 3RD - NOT OK, EMPTY ARRAY (deta is 100% pushing in the forEach loop) console.log(logData); return logData;};module.exports = { getAuditLogsData};async/await 承诺有什么问题吗?
添加回答
举报
0/150
提交
取消