Reputation: 253
trying to build a generic function that calls async functions dynamically.
//passing the controller and functionName (string)
function asyncAwaitCaller(controller, functionName) {
let result = await controller[functionName];
}
and in my controller :
async dummyFunction() {
return "dummy";
}
but I get the following error :
SyntaxError: await is only valid in async function
Is there a way around this, because this works fine with Promises.
Upvotes: 0
Views: 1220
Reputation: 1905
You need to use async
here:
async function asyncAwaitCaller(controller, functionName) {
let result = await controller[functionName];
}
Because you cannot use await if your parent function is not async
Upvotes: 3