title: Return value from page evaluation (puppeteer, asnyc programming)
body: <p>I've got the following problem:
  I have a page evaluation in puppeteer which includes asnychronous parts. I want to return the value from the asynchronous part to puppeteer, however, it just returns undefined without waiting for the Promise to resolve. Does anybody how to solve the problem?</p>
  
  <p>My Sample code:</p>
  
  <pre><code>const puppeteer = require('puppeteer');
  async function testing(num) {
    const browser = await puppeteer.launch({
      headless: false,
      ignoreHTTPSErrors: true
    });
    const page = await browser.newPage();
    const evaluating = await page.evaluate((num) =&gt; {
      //some synchrnous stuff (declaring some variablesand so on...)
      function lookForNumber(num) {
        if (num &gt; 2) {
          var asyncstuff = setTimeout(function () {
            if (num &gt; 10) {
              console.log('number is greater than 9');
              var whatIwantToRetrun = 'ten';
              return Promise.resolve(whatIwantToRetrun);
              //return here
            }
            if (num &gt; 5 &amp;&amp; num &lt; 10) {
              console.log('number is samller than 10');
              var whatIwantToRetrun = 'nine';
              return Promise.resolve(whatIwantToRetrun);
              //return here
            }
            else {
              num++;
              lookForNumber(num)
            }
          }, 2000);
        }
      }
      lookForNumber(num)
    }, num)
    console.log(evaluating); // returns undefined before function has finished
  }
  testing(4)
  </code></pre>
  
  <p>Puppeteers example: </p>
  
  <pre><code>const result = await page.evaluate(() =&gt; {
    return Promise.resolve(8 * 7);
  });
  console.log(result); // prints "56"
  </code></pre>
  
  <p><a href="https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageevaluatepagefunction-args" rel="nofollow noreferrer">Chromes API on evaluate</a></p>
  
  <p>According to <a href="https://github.com/GoogleChrome/puppeteer/issues/11" rel="nofollow noreferrer">this link</a> and the updated API, puppeteer always evaluates the code, and, if the evaluation is a promise, waits for the promise to resolve and returns the promise value.</p>
  
  <p>Thanks in advance for any help!</p>
  
  <p>Edit: I figured it out!</p>