I'm trying to use async/await for a very basic promise, but I'm getting the error: SyntaxError: await is only valid in async function. However, I believe I'm using await for an async function.
function getNumber(mult) { return new Promise((resolve, reject) => { resolve(10); }).then((val) => { return new Promise((resolve, reject) => { resolve(val * mult) //reject("Error"); }).then((val2) => val2); }).catch((err) => { return err; }) } const calculate = async (x) => await Promise.resolve(getNumber(x)) const val = await calculate(2)
const val = await calculate(2)must be in an async function.await calculateisn't in an async function as the error explains.calculatecould be simplygetNumber(x)as the extraasync/awaitandPromise.resolvedon't do anything, the manynew Promisesandthens seem pointless. I'm not sure I understand what you're hoping to accomplish.