Reputation: 45
So I just started learning puppeteer.js and what happens is that my code runs it runs without bugs but it does not display anything to the console. (I use Node.js for debugging purposes) The only time is does display something is when I put the async function inside another function and even then it return undefined. I was wondering why this is and how to fix it.
Here is my code:
(async () =>{
let movieUrl = 'https://www.imdb.com/title/tt0111161/?ref_=nav_sr_1'
let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto(movieUrl, {waitUntil: 'networkidle2'})
let data = await page.evaluate(() => {
let title = document.querySelector('div[class="title_wrapper"] > h1').innerText;
let rating = document.querySelector('span[itemprop="ratingValue"]').innerText;
let ratingCount = document.querySelector('span[itemprop="ratingCount"]').innerText;
return{title, rating, ratingCount};
})
console.log(data);
debugger;
await browser.close();
})
Upvotes: 1
Views: 106
Reputation: 16838
This is an unnamed async function:
async () => {
// the function code
}
If you want to run it straight away, you need to call it. You can do it by enclosing it in parentheses:
(async () => {
// the function code
})()
This is called IIFE, Immediately-invoked Function Expression. You declare a function and run it immediately.
See these articles to learn about it:
Upvotes: 1