title: Nodejs Puppeteer Wait to finish all code from loop
body: <p>I'm using puppeteer to scrap a webpage. In this webpage, i input some text and click the submit button. Once clicked, the page will return a table with results. I want to get these table results when matches 'somevar'. 
  The problem is: The for is not completing all functions before looping. Instead of filling the input and get the results first, it already goes to the next loop and fill the input again, resulting in something like: 'test1test2'</p>
  
  <p>How to make the for execute all functions inside before looping?</p>
  
  <pre><code>callPup();
  </code></pre>
  
  <p>async function callPup(){   </p>
  
  <pre><code>'use strict';
  
  const puppeteer = require('puppeteer');
  
  const textos = ['test1','test2'];
  
  (async() =&gt; {
      const browser = await puppeteer.launch({headless: false});
      const page = await browser.newPage();
  
      await page.goto('http://localhost/teste.html');     
  
      await page.waitForSelector('#input1').then(funcOk());
  
  
      async function funcOk(){            
          for (let i = 0; i &lt; textos.length; i++) {                       
  
              await page.type('#input1', textos[i]);
  
              await page.keyboard.press('Enter');                 
  
              /*get table results*/
              const data = page.evaluate(() =&gt; {
                  const tds = Array.from(document.querySelectorAll('table tr td a'))
                  return tds.map(a =&gt; {
                      var txt = a.innerHTML;
                      return txt.replace(/&lt;a [^&gt;]+&gt;[^&lt;]*&lt;\/a&gt;/g, '').trim();
                  });
              });
              /*get table results*/
  
              /*get only valid results*/
              let j = 0;
              for (let z = 0; z &lt; data.length; z++) {
                  if(data[z] == someVar[i].num.toString()){
                      j = j + 1;          
                  }
                  if(j &lt;= 14){
                      console.log(data[z]);
                      j = j + 1;
                  }
              }
              /*get only valid results*/
          }           
      }
  
  })();   
  </code></pre>
  
  <p>}</p>