I am having issues with how to handle data in an async fashion with Promises.
I have created a function that takes in a PDF stream and using pdf.js parses the document, pages and annotations to extract the values I need in an Array format.
The issue is that after I loop through the pages, and each annotation element, I am unable to update the outer scope variable.
The goal is to have dataArray populated with the records of each element of each page in the PDF document.
My codes is as below:
const pdfjs = require('pdfjs-dist');
function parsePdf(data) {
let dataArray = [];
pdfjs.getDocument(data)
.then(pdf => {
for (let i = 1; i <= pdf.numPages; i++) {
pdf.getPage(i)
.then(page => {
page.getAnnotations()
.then(items => {
items.forEach(element => {
let obj = {};
if (element.fieldName) {
obj.id = element.id;
obj.name = element.fieldName;
obj.value = element.fieldValue;
obj.isCheckbox = element.checkBox;
obj.isRadioButton = element.radioButton;
obj.isReadOnly = element.readOnly;
obj.type = element.fieldType;
dataArray.push(obj);
}
});
// Return values See example 1 below
console.log(dataArray);
});
});
}
});
// Returns Empty Array []
console.log(dataArray);
return dataArray;
}
I'd like to understand I I can get the values returned in the inner forEach scope to the outer variable in the function. I know it has to do with how Promises are handling the response but I just can't figure out how to chain the .then()
code.
Example of what comes back in each console.log(dataArray)
[
{ id: '59R',
name: 'Name_es_:signer:signature',
value: 'Jon Doe',
isCheckbox: undefined,
isRadioButton: undefined,
isReadOnly: false,
type: 'Tx' },
{ id: '62R',
name: 'Address',
value: '1 Main Street',
isCheckbox: undefined,
isRadioButton: undefined,
isReadOnly: false,
type: 'Tx' }
]
Thank you for your help!