简体   繁体   English

无法识别异步函数

[英]Async functions not being recognized

I have some async functions that I have used in multiple projects, however when I added them to another project they suddenly aren't working.我有一些我在多个项目中使用过的异步函数,但是当我将它们添加到另一个项目时,它们突然不起作用。

An example of one of these functions is:这些功能之一的示例是:

const ddbGet = async (params) => {
    try {
        const data = await docClient.get(params).promise();
        return data;
    } catch (err) {
        console.log("Failure", err.message);
        return false;
    }
};

The error that gets thrown is:抛出的错误是:

const ddbGet = async (params) => {

                     ^

SyntaxError: Unexpected token ( SyntaxError:意外的令牌(

However I know there are no problems with the functions' syntax as they are used successfully elsewhere.但是我知道函数的语法没有问题,因为它们在其他地方成功使用。

I have seen some answers to other questions that have suggested some issues with JSHint and ESLint however I don't believe I was using either of these, but just to be sure I installed ESLint and specified the ECMA version as suggested in those answers and this error was still thrown.我已经看到了一些其他问题的答案,这些答案暗示了 JSHint 和 ESLint 的一些问题,但是我不相信我正在使用其中任何一个,但只是为了确保我安装了 ESLint 并按照这些答案中的建议指定了 ECMA 版本,这仍然抛出错误。

I have also made sure I'm using the latest version of Node.js.我还确保我使用的是最新版本的 Node.js。

And if I remove those functions an error gets thrown due to an asynchronous function in the node_modules folder.如果我删除这些函数,则会由于 node_modules 文件夹中的异步 function 引发错误。

async handshake(transportName, req, closeConnection) {

   `^^^^^^^^^`

SyntaxError: Unexpected identifier SyntaxError:意外的标识符

Does anybody know what the problem could be?有谁知道问题可能是什么? Thanks.谢谢。

Since the documentation, or rather AWS Cloud9's Getting Started isn't kept up-to-date (using an old script to install NVM) and seeing that the project was very old (as per the extended comment section of the question), it surely was the NodeJS version.由于文档,或者更确切地说 AWS Cloud9 的入门没有保持最新(使用旧脚本安装 NVM)并且看到项目非常旧(根据问题的扩展评论部分),它肯定是 NodeJS 版本。

This is confirmed by the nvm ls output the OP gave us (I only formatted it): OP 给我们的nvm ls output 证实了这一点(我只对其进行了格式化):

v6.15.1
-> v17.2.0 system
default -> 6 (-> v6.15.1)
node -> stable (-> v17.2.0) (default)
stable -> 17.2 (-> v17.2.0) (default)
iojs -> N/A (default)
lts/* -> lts/argon (-> N/A) 
lts/argon -> v4.9.1 (-> N/A) 
lts/boron -> v6.17.1 (-> N/A) 
lts/carbon -> v8.17.0 (-> N/A) 
lts/dubnium -> v10.24.1 (-> N/A) 
lts/erbium -> v12.22.9 (-> N/A) 
lts/fermium -> v14.18.3 (-> N/A) 
lts/gallium -> v16.13.2 (-> N/A)

The line default -> 6 (-> v6.15.1) informs us that the default NodeJS version used (for the whole system) is v6.15.1 .default -> 6 (-> v6.15.1)告诉我们使用的默认 NodeJS 版本(用于整个系统)是v6.15.1

Unfortunately, NodeJS does NOT handle async/await syntax natively before v7.6 .不幸的是,NodeJS 在v7.6之前原生处理 async/await 语法。

You could run nvm install 17.2.0 and then nvm use 17.2.0 (or simply the nvm use part, but I don't know if the v17.2.0 we see in the output have effectively been installed via NVM), which is likely to fix this particular problem.您可以运行nvm install 17.2.0然后nvm use 17.2.0 (或者只是nvm use部分,但我不知道我们在v17.2.0中看到的 v17.2.0 是否已通过 NVM 有效安装),这很可能解决这个特殊问题。

I've found the solution with a little more research after Gaëtan Boyals put me on the right path!在 Gaëtan Boyals 让我走上正确的道路后,我通过更多研究找到了解决方案!

He was correct that the default version of Node.js was the issue and the solution to fix this was nvm alias default 17.2.0他是正确的 Node.js 的默认版本是问题,解决这个问题的解决方案是nvm alias default 17.2.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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