Hunter
Hunter

Reputation: 123

Web Scraping Loop w/ Puppeteer: "await is only valid in async function"

I'm trying to check what the current item on air is at qvc.com in a repeating loop using the following code, however I get "await is only valid in async function" on the line "const results = await..."

Here is my code:

(async () => {
    // Init
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.qvc.com/content/iroa.qvc.eastern.html');

    // Selectors
    const current_item_selector = '.galleryItem:first-of-type a';

    // Functions
    setInterval(function() { // Repeat every 5s
        const results = await page.$(current_item_selector);
        const item = await results.evaluate(element => element.title);
        console.log(item);
    }, 5000);
})();

UPDATE: setTimeout was supposed to be setInterval that was my mistake, a copy/paste error. I updated that in the codeblock, thanks those that pointed that out.

Upvotes: 0

Views: 391

Answers (1)

domenikk
domenikk

Reputation: 1743

The function inside setInterval needs to be async as well:

// Functions
    setInterval(async function() { // Repeat every 5s
        const results = await page.$(current_item_selector);
        const item = await results.evaluate(element => element.title);
        console.log(item);
    }, 5000);

Upvotes: 1

Related Questions