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
1 Answer
Reset to default 6The 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)