尝试构建一个动态调用异步函数的通用函数。
//passing the controller and functionName (string)
function asyncAwaitCaller(controller, functionName) {
let result = await controller[functionName];
}
在我的 Controller 中:
async dummyFunction() {
return "dummy";
}
但我收到以下错误:
SyntaxError: await is only valid in async function
有没有办法解决这个问题,因为这对于 Promises 来说效果很好。
最佳答案
您需要使用async
这里:
async function asyncAwaitCaller(controller, functionName) {
let result = await controller[functionName];
}
因为如果您的父函数不是异步
,则无法使用await
关于javascript - Node js 中的动态异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49174814/