V
V
vsn132018-06-08 16:35:46
JavaScript
vsn13, 2018-06-08 16:35:46

How to use setInterval inside async/await?

Sorry for the stupid question, but I'm not at all familiar with all this asynchrony.
How can I use spacing inside code like this (code below under the spoiler)? This is a Puppeteer that launches a headless browser, and logs in to the site from where I need to pull the data once every 500ms, something like this:

Screen
QjusF8x.png

I need to write this data somewhere to a local file. In general, here is the code, but for some reason it is the interval part that does nothing and does not output to the console, although I expected that it would pull data from this selector every 500ms and output it. Also, no errors.
The code
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://url.com');

    // Auth
    await page.click(selector.login.tab);
    await page.click(selector.login.email);
    await page.keyboard.type(settings.username);
    await page.click(selector.login.password);
    await page.keyboard.type(settings.password);
    await page.click(selector.login.button);

    // Waiting for loading
    await page.waitFor(10000); // TO FIX: there we should wait for loaded element or selector
    await page.evaluate(() => {
            setInterval(() => {
                return console.log(document.querySelector('div._captions > p._value').innerText + document.querySelector('div._captions p._hl-value').innerText);
            }, 500);
        });

    //await page.screenshot({path: 'screenshot.jpg'});
    await browser.close();
})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Rybin, 2018-06-08
@drfisher

await page.evaluate(() => {
  return new Promise((resolve) => {
    setInterval(() => {
      console.log(document.querySelector('div._captions > p._value').innerText + document.querySelector('div._captions p._hl-value').innerText);
      resolve();
    }, 500);
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question