在exceljs包中的worksheet.eachRow()中调用一个带有wait的异步函数


const Excel = require("exceljs");
const fs = require("fs");
const createConnection = require("./db.connection");
const SEGMENT = require("./models/segment");
const fileName = "test.xlsx";
createConnection(); //Db connection
const readExcel = async () => {
try {
let workbook = new Excel.Workbook();
await workbook.xlsx.readFile(fileName);
readRows(workbook);
} catch (error) {
console.log("error", error);
}
};
readExcel();
readRows = (workbook, headers) => {
workbook.eachSheet((worksheet, sheetId) => {
// console.log(worksheet);
worksheet.eachRow({ includeEmpty: false }, async (row, rowNumber) => {
console.log("Entered ROW at LOC 116", rowNumber);
const segment = new SEGMENT({ versionName: row.getCell(1).text.trim() });
const saveSegment = await segment.save(); //Insert into db
console.log("Entered ROW at LOC 119 Save Segment", saveSegment); //Further code after this line
});
});
};

我只想等到文档保存在数据库中后,再在workbook.eachRow循环中进行进一步处理。我得到的输出是

Entered ROW at LOC 116 1
Entered ROW at LOC 116 2
Entered ROW at LOC 116 3
Entered ROW at LOC 116 4
Entered ROW at LOC 116 5
Entered ROW at LOC 116 6
Entered ROW at LOC 116 7
Entered ROW at LOC 116 8
Entered ROW at LOC 116 9
Entered ROW at LOC 116 10
Entered ROW at LOC 116 11
Entered ROW at LOC 116 12
Entered ROW at LOC 116 13
Entered ROW at LOC 116 14
Entered ROW at LOC 116 15
Entered ROW at LOC 116 16
Entered ROW at LOC 116 17
Entered ROW at LOC 116 18
Entered ROW at LOC 116 19
Entered ROW at LOC 116 20
Entered ROW at LOC 116 21
Entered ROW at LOC 116 22
Entered ROW at LOC 116 23
Entered ROW at LOC 116 24
Entered ROW at LOC 116 25
Entered ROW at LOC 116 26
Entered ROW at LOC 116 27
Entered ROW at LOC 116 28
Entered ROW at LOC 116 29
Entered ROW at LOC 116 30
Entered ROW at LOC 116 31
Entered ROW at LOC 116 32
Entered ROW at LOC 116 33
Entered ROW at LOC 116 34
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f92,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f93,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}

这意味着每一行都将被执行,直到我调用await内部循环之前。而我想要的是,由于工作簿,每行的整个处理都应该在移动到第二行之前完成。每行是一个同步函数。基本调用输出应该是这样的。

Entered ROW at LOC 116 1
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f92,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}
Entered ROW at LOC 116 2
Entered ROW at LOC 119 Save Segment {
isDeleted: false,
isActive: true,
_id: 62d714775d9c0d736f0d2f93,
versionName: 'versionName',
createdAt: 2022-07-19T20:30:47.583Z,
updatedAt: 2022-07-19T20:30:47.583Z
}

这与我们在forEach循环中使用async/await时遇到的问题相同吗?有解决办法吗?

实现它的一种方法是使用for循环:

for (let i = 1; i <= worksheet.rowCount; i++) {
const row = worksheet.getRow(i)
}

另一种方法可能是用承诺包装eachRow调用:

await new Promise((resolve) => {
worksheet.eachRow(async (row, i) => {
const result = await someAsyncFn()
if (i === worksheet.rowCount) {
resolve()
}
})
}

您可以使用await Promise.all(Promise)来等待所有行被正确处理。

const someAsyncFn = async(value) => {
console.log('executing: ' + value);
return value;
};
var promises = [];
// same as worksheet.eachRow
[1, 2, 3, 4].forEach(p => {
promises.push(someAsyncFn(p).then(p => {
console.log('finished call:' + p);
}));
});
Promise.all(promises).then(p => console.log('all rows are finished'));

相关内容

  • 没有找到相关文章

最新更新