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

Viewed 152

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); 
1 Answers

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); 
Related