0

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.

Hunter
  • 73
  • 1
  • 4
  • The error is exactly as it says it is - your `setTimeout` function isn't `async` – Adam Jenkins Dec 15 '20 at 22:19
  • 1
    setTimeout only runs once, did you mean setInterval? – Alexander Staroselsky Dec 15 '20 at 22:20
  • Just be prepared, even with that change you will likely have issues with trying to `await` operations in a setInterval. See https://stackoverflow.com/questions/51830200/how-to-await-inside-setinterval-in-js and https://stackoverflow.com/questions/33289726/combination-of-async-function-await-settimeout – Alexander Staroselsky Dec 15 '20 at 22:30
  • Your browser will close before the first interval. You're probably better off with waitForTimeout in a while loop. – pguardiario Dec 16 '20 at 02:49

1 Answers1

1

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);
domenikk
  • 1,723
  • 1
  • 10
  • 12
  • Not sure, but the comments in the question say they need it to repeat every x seconds. setTimeout would not be the correct function for that. The question also mentions a "repeating loop", this solves only part of the problem. – Alexander Staroselsky Dec 15 '20 at 22:21
  • 1
    @AlexanderStaroselsky - you're correct, but the answer addresses the error the OP is asking about, there was no mention (yet) of desired functionality not being achieved. – Adam Jenkins Dec 15 '20 at 22:22
  • That's a separate issue imo, but I'll edit the answer to account for it – domenikk Dec 15 '20 at 22:23
  • Thanks for pointing out the setInterval vs setTimeout that was a copy/paste error on my part when drafting the post. Adding async to the function was the answer, however the ultimate desired effect isn't being achieved yet. I was under the assumption puppeteer was supposed to simulate a browser in node, but it seems this is just repeating the same result, as if it is just a static page load. Is there a better way/package to use to do this? – Hunter Dec 15 '20 at 22:41
  • @Hunter you can try to reload the page every 5s: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagereloadoptions – domenikk Dec 15 '20 at 22:45
  • @domenikk I put a reload function at the top of the interval, thank you : ) – Hunter Dec 23 '20 at 09:49