我是 ES6 和 Promise 的新手。我正在尝试 pdf.js 将 pdf 文件的所有页面中的文本提取到字符串数组中。提取完成后,我想以某种方式解析数组。说pdf文件(通过 typedarray
正确传递)有 4
页面,我的代码是:
let str = [];
PDFJS.getDocument(typedarray).then(function(pdf) {
for(let i = 1; i <= pdf.numPages; i++) {
pdf.getPage(i).then(function(page) {
page.getTextContent().then(function(textContent) {
for(let j = 0; j < textContent.items.length; j++) {
str.push(textContent.items[j].str);
}
parse(str);
});
});
}
});
它设法工作,但是,当然,问题是我的 parse
函数被调用 4
次。我只想在所有 4 页提取完成后调用 parse
。
原文由 Sangbok Lee 发布,翻译遵循 CC BY-SA 4.0 许可协议
类似于 https://stackoverflow.com/a/40494019/1765767—— 使用 Promise.all 收集页面承诺并且不要忘记链接然后是: