简体   繁体   中英

Issue with NodeJS async/await - accessing function parameter

I am trying to do some scraping using a library and my code uses Node's async/await pattern.

I have defined a variable 'page' in function named 'sayhi' and I pass the same variable to function ex, I get error while running the code.

const puppeteer = require('puppeteer');

async function sayhi() {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto('https://www.example.com/'); // 
  ex(page); //FAILS

  var frames2 = await newpage.frames(); // WORKS
}

function ex(newpage){
  var frames = await newpage.frames(); // FAILING
}

sayhi();

You're using await in a function that isn't an async function. Try this instead:

async function ex(newpage) {

If you need frames2 to run only after ex is finished completely, you'll also want to await ex(page); in sayhi .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM