0

I'm receiving the following error:

ReferenceError: processElement is not defined

When trying to invoke this regular function from my main async function.

I'm using Chrome Puppeteer to get information about page elements. Puppeteer wants to run in an async function, understandably, but I need to do some processing in other functions, possibly recursively.

My basic structure is this:

function processElement(element, index) {
  // element processing here, may ultimately need recursion.
}

function async main() {
  // puppeteer stuff
  const elements = document.querySelectorAll('p');
  elements.forEach((element, index) => {
    processElement(element, index);
  }
}

main();

Thanks for any help! I'm new to the whole async/await paradigm.

Seth Wilson
  • 241
  • 1
  • 4
  • 13

1 Answers1

1

You need to use the async keyword before the function.

function processElement(element, index) {
  // element processing here, may ultimately need recursion.
  console.log(element);
}

async function main() {
  // puppeteer stuff
  const elements = document.querySelectorAll('p');
  elements.forEach((element, index) => {
    processElement(element, index);
  });
}

main();
<p>Hello</p>
<p>World</p>
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44