I'm trying to implement a pdf word count in Javascript. I came across pdf.js which uses promises. Is there a way to wait till the script is done before returning the count? I know that this goes against the idea of promises, but the other js pdf readers out there either sometimes produce a bunch of gibberish or return nothing. In its current form the function always return a word count of 0.
function countWords(pdfUrl){
var pdf = PDFJS.getDocument(pdfUrl);
var count = 0;
pdf.then(function(pdf) {
var maxPages = pdf.pdfInfo.numPages;
for (var j = 1; j <= maxPages; j++) {
var page = pdf.getPage(j);
var txt = "";
page.then(function(page) {
var textContent = page.getTextContent();
textContent.then(function(page){
for(var i=0;i<page.items.length;i++){
txtadd = page.items[i].str
txt += txtadd.replace(/[^a-zA-Z0-9:;,.?!-() ]/g,'');
}
count = count + txt.split(" ").length;
})
})
}
return count;
});
}