异步函数中的给电子错误

Electron giving error in asynchronous functions

我正在使用 Electron 并编写了以下代码:

async function fetchPage({region , role, tier}){
  return fetch(
    `https://example.com/`
  )
    .then((res) => res.text())
    .then((text) => cheerio.load(text));
}
(async() => {
 $ = await fetchPage({region: region, role: role, tier: tier});
})();  

我收到这个错误:

async function fetchPage({region , role, tier}){
      ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:76:16)

这个街区有什么问题?

当我尝试编写 await/async 块时只有 Electron 发出错误

Solution

我首先卸载/删除项目的当前电子版本。

npm uninstall electron (At the dir of the project)
npm uninstall -g electron

之后我根据@jfriend00的回复安装了最新版本的electron

npm install electron@latest --save-dev (At the dir of the project)
npm install -g electron@latest

您安装的电子副本中内置的 nodejs 版本似乎已过时,因此它的 nodejs 版本尚不支持 asyncawait .

更新你的 nodejs 版本最简单的方法是像这样更新整个电子:

npm install electron@latest

electron当前v12.0.9版本应该包含nodejs v14.16.