John Yepthomi
John Yepthomi

Reputation: 502

How can I pass variable into an evaluate function that evaluates an async function using Puppeteer?

I'm using Puppeteer. The variable passed doesn't work. I need this variable to be used inside the browser's context. Here's the stripped down version of the code :

let currentPost = 1;

await page.evaluate(async (currentPost) => {
    await new Promise((currentPost, resolve, reject) => {
        
        var timer = setInterval(() => {

            console.log(currentPost);
            resolve();
            
        }, 100);

    }); 

}, currentPost); 

Upvotes: 2

Views: 679

Answers (1)

Ran Turner
Ran Turner

Reputation: 18036

Promise accepts in two arguments: resolve and reject. remove the current Post arg and it will work as expected.

let currentPost = 1;

await page.evaluate(async (currentPost) => {
    await new Promise((resolve, reject) => {
        
        var timer = setInterval(() => {

            console.log(currentPost);
            resolve();
            
        }, 100);

    }); 

}, currentPost); 

Upvotes: 3

Related Questions