I am trying to write a page's content into a list with puppeteer.
My problem is too basic for other solutions out there. The reason I am failing is because I am too ignorant to get the resolved promise from an asynchronous function. I looked at the js documentation, I looked at similar answers, to no avail.
I also tried multiple ways to resolve the page.content()
promise within the asynchronous function, still failing miserably.
async function getPageContent(website) {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(website, { waitUntil: "networkidle2" });
const websiteContent = await page.content();
await browser.close();
return websiteContent;
}
var htmlList = [];
var ws = "/";
var googleHtml = getPageContent(ws);
htmlList.push(googleHtml);
console.log(htmlList);
The result is [Promise { pending }]
, which I understand is due to the promise status being unresolved. What I would like is the actual string of google's DOM.
Thank you for any help in this specific promise / async / puppeteer crash course.
I am trying to write a page's content into a list with puppeteer.
My problem is too basic for other solutions out there. The reason I am failing is because I am too ignorant to get the resolved promise from an asynchronous function. I looked at the js documentation, I looked at similar answers, to no avail.
I also tried multiple ways to resolve the page.content()
promise within the asynchronous function, still failing miserably.
async function getPageContent(website) {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(website, { waitUntil: "networkidle2" });
const websiteContent = await page.content();
await browser.close();
return websiteContent;
}
var htmlList = [];
var ws = "https://www.google./";
var googleHtml = getPageContent(ws);
htmlList.push(googleHtml);
console.log(htmlList);
The result is [Promise { pending }]
, which I understand is due to the promise status being unresolved. What I would like is the actual string of google's DOM.
Thank you for any help in this specific promise / async / puppeteer crash course.
Share Improve this question edited Aug 13, 2019 at 15:51 hardkoded 21.8k3 gold badges61 silver badges74 bronze badges asked Aug 13, 2019 at 15:02 GregKGregK 5936 silver badges16 bronze badges 01 Answer
Reset to default 4You have to await getPageContent
:
var htmlList = [];
var ws = "https://www.google./";
var googleHtml = await getPageContent(ws);