3 回答
TA贡献2041条经验 获得超4个赞
答案只是将函数包装在 async 中并使用await. 我猜你无法在async🤔之外获取此数据
async function getTableDetails(paths, rowStart = 2) {
let tables = {};
for (var i = 0; i < paths.length; i++) {
let p = paths[i];
const getRows = async (fileName) => {
const csvData = await csvToJson({
delimiter: "\t",
trim: true
}).fromFile(fileName);
return csvData;
};
let rows = await getRows(p);
tables[p] = { rows: rows };
}
return tables;
}
(async function test() {
let tableDetails = await getTableDetails(fps, rowStart=2);
console.log(Object.keys(tableDetails));
doSomething(tableDetails);
})();
TA贡献2011条经验 获得超2个赞
尝试这个
const csvToJson = require("csvtojson");
const getRows = async (fileName) => {
const csvData = await csvToJson({
delimiter: "\t",
trim: true
}).fromFile(fileName);
return csvData;
};
getRows("myfile.tsv").then(data=>{
console.log(data.length);
}
)
TA贡献1831条经验 获得超4个赞
你可以这样做
const csvToJson = require("csvtojson");
const getRows = async (fileName) => {
const csvData = await csvToJson({
delimiter: "\t",
trim: true
}).fromFile(fileName);
const rows = [];
csvData.forEach((row) => {
rows.push(row);
});
return rows;
};
getRows("myfile.tsv").then((rows) => {
console.log(rows.length);
});
添加回答
举报