i have created below code to get some data, i am returning that data in a function and i am calling that function in a variable like below :
let tmsValue = await GetTMS(fymcl,this.model);
console.log("tmsValue",tmsValue);
let tms;
var GetTMS = async function getTMS(yearMetricCombo, modelData) : Promise<any>{
return new Promise((resolve, reject)=>{
modelData
.query("11111")
.where("fymcl").equals(yearMetricCombo) // tm|FY18|Promtions
.loadAll()
.exec((error, data) => {
if (error) {
reject(error);
} else {
tms = String(data.Items[0].attrs.uploadtms);
resolve(tms);
// console.log("tms",tms); // iam getting value over here
}
});
return tms;
});
}
The issue is let tmsValue = await GetTMS(fymcl,this.model); // this line is saying 'await' expression is only allowed within an async function.
I am not sure what i need to do.
GetTMShas no return statement, so the function implicitly returnsundefined. It looks like it's making an asynchronous db call, so you may want to look into using aPromisehere. Take a look at this questionawaitkeyword can only be used in a function declared with theasynckeyword. That means it cannot appear at the root level of your file. If you need to use thegetTMSfunction at the root of the file, you'll have to put the code that uses its result inside the.thencallback of the promise. If you need to usegetTMSin another function, you either need to use the.thenmethod as mentioned, or mark the function asasync.