【发布时间】:2021-09-07 16:21:26
【问题描述】:
我正在制作一个包含三个不同功能的程序:
- downloadPDF:从网上下载 PDF
- getPDF: 读取并解析 pdf
- getData: 循环通过 getPDF
我遇到的问题是第三个函数 (getData) 有一个运行 getPDF 的 for of 循环,似乎在尝试 console.log getPDF 返回的结果之前它没有让 getPDF 完成.
下面是三个函数:
async function downloadPDF(pdfURL, outputFilename) {
let pdfBuffer = await request.get({uri: pdfURL, encoding: null});
console.log("Writing downloaded PDF file to " + outputFilename + "...");
fs.writeFileSync(outputFilename, pdfBuffer);
}
async function getPDF(query, siteName, templateUrl, charToReplace) {
const currentWeek = currentWeekNumber().toString();
await downloadPDF(templateUrl.replace(charToReplace, currentWeek), "temp/pdf.pdf");
var resultsArray = []
let dataBuffer = fs.readFileSync("temp/pdf.pdf");
pdf(dataBuffer).then(function(data) {
pdfContent = data.text;
const splittedArray = pdfContent.split("\n");
const parsedArray = splittedArray.map((item, index) => {
if(item.includes(query)) {
resultsArray.push({result: item, caseId: splittedArray[index-1].split(',', 1)[0], site: siteName});
}
}).filter(value => value);
return(resultsArray);
});
fs.unlinkSync("temp/pdf.pdf"); //deletes the downloaded file
}
async function getData(query, desiredSites) {
var resultsArray = []
for (const value of desiredSites) {
let result = await getPDF(query, sitesList.sites[value].name, sitesList.sites[value].templateUrl, sitesList.sites[value].charToReplace);
console.log(result)
}
}
getData("test", ['a', 'b']);
在底部函数(getData)中,console.log 结果为undefined
我猜这与承诺有关。有任何想法吗?非常感谢!
【问题讨论】:
-
sitesList来自哪里? -
在
getPDF中调用了一个名为pdf的函数。这似乎是一个异步函数(特别是一个承诺),因为您调用了then。这个功能根本不需要等待。同样getPDF似乎没有返回任何东西,所以result将出现undefined。 -
对于投票关闭作为重复的人来说,这并不是真正的重复。异步循环不是问题——OP 已经完美地实现了这一点。问题出现在堆栈的更深处。我可能仍会投票关闭,但“不可复制或由拼写错误引起”,因为该决议不太可能对未来的读者有所帮助。
-
@Wing 我在 getPDF 的末尾做
return(resultsArray);。 -
返回值在传递给
then的函数内部。它不会从getPDF返回。请参阅How to return the response from an asynchronous call? 或Return from a promise then()。
标签: javascript for-loop async-await promise