xwedea
xwedea

Reputation: 25

Node.js Puppeteer How To Await For page.evaluate() Method

I need to wait for scrolling action to finish, then resolve.

When I put resolve() inside page.evaluate() block it throws: (node:22646) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: resolve is not defined

If I keep it like this It resolves immediately.

let scrollingPromise = new Promise((resolve, reject) => {
    page.evaluate(() => {
        const scrollingWindow = document.querySelector('.section-layout.section-scrollbox.scrollable-y.scrollable-show');            
        for (let i = 0; i < 4; i++) {
            setTimeout(function () {
                scrollingWindow.scrollBy(0, 3000);
            }, 2000 * i);
        }
    });
    resolve();
});

SOLVED https://github.com/puppeteer/puppeteer/issues/844

Upvotes: 0

Views: 1113

Answers (1)

Dmitriy Mozgovoy
Dmitriy Mozgovoy

Reputation: 1597

I removed the for loop, don't know what it is there for

function scroll(){
    return page.evaluate(() => {
        const scrollingWindow = document.querySelector('.section-layout.section-scrollbox.scrollable-y.scrollable-show');
        return new Promise(resolve=>{
            setTimeout(function () {
                scrollingWindow.scrollBy(0, 3000);
                resolve();
            }, 2000);
        });
    });
}

Upvotes: 0

Related Questions