【发布时间】:2020-11-25 12:16:25
【问题描述】:
我有一个旧的 AWS Lambda 函数,它被声明为同步(使用 Promises),声明如下所示:
exports.getSong = (event, context, callback) => { }
它按预期工作。最近,我决定使用 async/await 重写它,因此尝试异步声明 getSong 函数,如下所示:
exports.getSong = async (event, context) => { }
在尝试执行时,我收到以下错误:
{
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Unexpected token '.'",
"trace": [
"Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '.'",
" at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1133:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)",
" at Module.load (internal/modules/cjs/loader.js:977:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:877:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
" at internal/main/run_main_module.js:18:47"
]
}
从这个错误消息中绝对不清楚问题是什么,但是通过谷歌搜索类似的问题并缩小范围,我发现问题出在函数声明上。
我尝试将getSong 函数声明为同步函数,然后在其中运行另一个异步函数,如下所示:
var anotherAsyncFunction = async () => { }
exports.getSong = (event, context, callback) => { anotherAsyncFunction() }
但是,我得到了同样的错误。很明显,它与 Lambda 中的异步函数声明有关。这里可能是什么问题?谢谢。
【问题讨论】:
-
看来您需要添加更多代码供我们调试(即第 98 行左右)
-
我没有 98 行代码。这就是为什么我说错误消息没有帮助。
-
上面的代码是你的 lambda 的唯一代码吗?
-
您在代码底部的
getSong函数不是异步的...您希望使用await 处理anotherAsyncFunction还是将.then与callback结合使用
标签: javascript node.js amazon-web-services aws-lambda async-await