Aniruddha Gohad
Aniruddha Gohad

Reputation: 253

dynamic async await in node js

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

Answers (1)

filipbarak
filipbarak

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

Related Questions