return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Return value from page evaluation (puppeteer, asnyc programming) - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return value from page evaluation (puppeteer, asnyc programming) - Stack Overflow

programmeradmin3浏览0评论

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?

My Sample 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) => {
    //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)

Puppeteers example:

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

Chromes API on evaluate

According to this link 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.

Thanks in advance for any help!

Edit: I figured it out!

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?

My Sample 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) => {
    //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)

Puppeteers example:

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

Chromes API on evaluate

According to this link 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.

Thanks in advance for any help!

Edit: I figured it out!

Share Improve this question edited Oct 21, 2017 at 13:08 Noah asked Oct 2, 2017 at 11:56 NoahNoah 6812 gold badges8 silver badges17 bronze badges 2
  • i suggest you to, reedit your question. And answer your own question, then mark it as an answer, it's permitted. – Adi Prasetyo Commented Oct 21, 2017 at 7:22
  • done! (10 characters) – Noah Commented Oct 21, 2017 at 13:09
Add a ment  | 

1 Answer 1

Reset to default 6

The solution to my problem:

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)
发布评论

评论列表(0)

  1. 暂无评论