Async throwing SyntaxError: Unexpected token (

こ雲淡風輕ζ 提交于 2019-12-10 18:39:42

问题


I'm running a test using the headless Chrome package Puppeteer:

const puppeteer = require('puppeteer')

;(async() => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://google.com', {waitUntil: 'networkidle'})
  // Type our query into the search bar
  await page.type('puppeteer')

  await page.click('input[type="submit"]')

  // Wait for the results to show up
  await page.waitForSelector('h3 a')

  // Extract the results from the page
  const links = await page.evaluate(() => {
    const anchors = Array.from(document.querySelectorAll('h3 a'))
    return anchors.map(anchor => anchor.textContent)
  })
  console.log(links.join('\n'))
  browser.close()
})()

And I'm running the script as: node --harmony test/e2e/puppeteer/index.js (v6.9.1)

But I get this error:

;(async() => {
       ^
SyntaxError: Unexpected token (

What could be the problem?

Note: I'm using Vue CLI's official Webpack template:


回答1:


I found out: node LTS (AKA node 6) is not supporting async / await mechanism right now. See :

See here for details : https://www.infoq.com/news/2017/02/node-76-async-await




回答2:


I tried your code on my laptop after a lint and it worked perfectly:

Maybe you have an environment issue.

have you considered removing the semi colon at the beginning of the line? It does not look like right programing. Or maybe a webpack issue.



来源:https://stackoverflow.com/questions/46066259/async-throwing-syntaxerror-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!