0

I need to scroll down before doing some action. my promise function is not waiting in await line. how can I solve this problem?

let scrollingPromise = new Promise(async (resolve, reject) => {

    // I need this part to be done before resolving
    await page.evaluate(() => {
        const scrollingWindow = document.querySelector('.section-layout.section-scrollbox.scrollable-y.scrollable-show');
        var scrollCount = 0;

        function scrollFunction(){
            setTimeout(()=>{
                scrollingWindow.scrollBy(0, 5000);
                scrollCount++;
                if(scrollCount < 5){
                    scrollFunction();
                }
            }, 3000);
        };

        scrollFunction();

    });
    // but it resolves instantly doesn't wait for previous await
    resolve();
});

//this .then() runs before my scrolling
scrollingPromise.then(async () => {
    // ...
}
xwedea
  • 25
  • 5
  • 1
    It doesn't await because it doesn't return a promise – Liam Oct 21 '20 at 14:33
  • 1
    Does this answer your question? [Combination of async function + await + setTimeout](https://stackoverflow.com/questions/33289726/combination-of-async-function-await-settimeout) – Liam Oct 21 '20 at 14:33
  • You should return a promise, so your `new Promise` should be what you want to return inside your callback, and the `await` it – Joan Albert Oct 21 '20 at 14:35

1 Answers1

2

You're mixing await and Promise syntaxes. You're awaiting an asynchronous function that doesn't return a Promise, so it can't work. I believe you're trying to do this :

let scrollingPromise = new Promise((resolve, reject) => {
    page.evaluate(() => {
        const scrollingWindow = document.querySelector('.section-layout.section-scrollbox.scrollable-y.scrollable-show');
        var scrollCount = 0;

        function scrollFunction() {
            setTimeout(() => {
                scrollingWindow.scrollBy(0, 5000);
                scrollCount++;
                if (scrollCount < 5) {
                    scrollFunction();
                } else {
                    resolve() // <-- Done, resolve here
                }
            }, 3000);
        };

        scrollFunction();
    });
});
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • How do you know this? – Jeremy Thille Oct 22 '20 at 11:24
  • I tested it. function does not reach to else condition. There is probably something wrong with my recursive function. I am trying to solve this problem now can u help me? https://stackoverflow.com/questions/64477989/node-js-puppeteer-how-to-await-for-page-evaluate-method – xwedea Oct 22 '20 at 11:45
  • But where does it stop? Did you try adding `console.log()` everywhere to see what gets logged and where it gets clogged? – Jeremy Thille Oct 22 '20 at 12:37
  • I did. it reaches to else and console.log() in else works but it throws error after that: Uncaught ReferenceError: resolve is not defined... If I change resolve() to Promise.resolve() it doesn't throw error but .then() still doesn't run. – xwedea Oct 22 '20 at 13:17
  • 1
    Oooh, I suspect `page.evaluate` to run within the scope of Puppeteer (or whatever headless browser you're using), so indeed, resolve() is undefined in this scope. Have a look at this : https://github.com/puppeteer/puppeteer/issues/844 – Jeremy Thille Oct 22 '20 at 15:10
  • This solved my problem. I will add this link to other question as a solution. Thanks a lot. – xwedea Oct 23 '20 at 09:17
  • Ah awesome! Glad you made it :) – Jeremy Thille Oct 23 '20 at 11:37