title: Check if a promise finished in Javascript?
body: <p>I am using PDF.js to extract text content from a PDF which I will use next for some more processing, For this,</p>
  
  <pre><code>var complete=0;
  var full_text="";
  var PDF_render = PDFJS.getDocument("x.pdf").then(function(pdf) {
      var page_text = {};
      for (i = 1; i &lt;= pdf.numPages; i++){
          pdf.getPage(i).then( function(page){
             var n = page.pageNumber;
              page.getTextContent().then( function(textContent){
                  var page_text_part = "";
                  textContent.items.forEach(function (textItem){
                      page_text_part += textItem.str;
                      page_text_part += " ";
                      });
                  page_text[n] =  page_text_part + "\n\n";
                  ++complete;
                  if (complete == pdf.numPages){
                   for( var j = 1; j &lt;= pdf.numPages; j++)
                      full_text += page_text[j];
                  }
                });
              });
          }
      });
  </code></pre>
  
  <p>The issue is that PDF.js returns promises and they are executed asynchronously, however I need to perform some post processing on the returned text. For this I need to wait for the promises to fully execute and only then move on. How does one achieve this? Please help.</p>