Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puppeteer evaluate functional with callback

I am trying to evaluate a function with puppeteer but the callback is never firing (i am certain the host page is working as expected).

On the host page a listener works like:

DB.when('ready').execute(function(db){
  // DB can execute stuff
})

My puppeteer code attempts to fetch the db that is ready:

try {
  const dbhandle = await page.evaluate('DB.when("ready")');
  const result = await page.evaluate(db => db.execute, function(images) {
    console.log(JSON.stringify(images));
    //do stuff with callback
  }, dbhandle);
    console.log('result', JSON.stringify(result));
} catch (e) {
    console.log('evaluate', e);
} finally {
    console.log('finally');
}

Am having no luck on this.

like image 343
Blair Anderson Avatar asked Mar 29 '26 22:03

Blair Anderson


1 Answers

OMG I figured it out...

try {
  function fooBugger() {
    return new Promise((resolve, reject) => {
      DB.when('ready').execute(function(db) {
        if (db) {
          resolve(db.some_data);
        } else {
          reject('nope');
        }
      });
    });
  }
  const res = await page.evaluate(fooBugger);

  console.log('resultHandle', JSON.stringify(res));
} catch (e) {
  console.log('evaluateHandle', e);
} finally {
}
like image 154
Blair Anderson Avatar answered Apr 02 '26 02:04

Blair Anderson