Return 来自页面评估的值(操纵者,异步编程)

Return value from page evaluation (puppeteer, asnyc programming)

我遇到了以下问题: 我在 puppeteer 中有一个页面评估,其中包括异步部分。我想 return 从异步部分到 puppeteer 的值,但是,它只是 return 未定义而没有等待 Promise 解决。有没有人如何解决这个问题?

我的示例代码:

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) => {
    //some synchrnous stuff (declaring some variablesand so on...)
    function lookForNumber(num) {
      if (num > 2) {
        var asyncstuff = setTimeout(function () {
          if (num > 10) {
            console.log('number is greater than 9');
            var whatIwantToRetrun = 'ten';
            return Promise.resolve(whatIwantToRetrun);
            //return here
          }
          if (num > 5 && num < 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)

木偶师示例:

const result = await page.evaluate(() => {
  return Promise.resolve(8 * 7);
});
console.log(result); // prints "56"

Chromes API on evaluate

根据 this link 和更新后的 API,puppeteer 总是评估代码,如果评估是一个 promise,则等待 promise 解决,return承诺值。

在此先感谢您的帮助!

编辑:我明白了!

我的问题的解决方案:

const puppeteer = require('puppeteer');
let date = require('date-and-time');
async function testing(num) {
  const browser = await puppeteer.launch({
    headless: true,
    ignoreHTTPSErrors: true
  });
  const page = await browser.newPage();
  await console.log('starting evaluation');
  let now = new Date();
  let time = date.format(now, 'YYYY/MM/DD HH:mm:ss');
  console.log(time);
  const test = await page.evaluate(async (num) => {
    console.log('starting evaluation');
    //some synchrnous stuff (declaring some variablesand so on...)
    function lookForNumber(num) {
      return new Promise((resolve, reject) => {
        if (num > 2) {
          var asyncstuff = setTimeout(function () {
            if (num > 10) {
              console.log('number is greater than 9');
              var whatIwantToReturn = 'ten';
              resolve(whatIwantToReturn);
            }
            if (num > 5 && num < 10) {
              console.log('number is samller than 10');
              var whatIwantToReturn = 'nine';
              resolve(whatIwantToReturn);
            }
            else {
              num++;
              lookForNumber(num)
            }
          }, 5000);
        }
      });
    }
    var returnvalue = await lookForNumber(num);
    return returnvalue;
  }, num)
  console.log(test);
  now = new Date();
  time = date.format(now, 'YYYY/MM/DD HH:mm:ss');
  console.log(time);
  await browser.close();
}
testing(6)