我创建了下面的代码来获取一些数据,我在函数中返回该数据,我在一个变量中调用该函数,如下所示:
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;
});
}
问题是let tmsValue = await GetTMS(fymcl,this.model); // this line is saying 'await' expression is only allowed within an async function.
我不确定我需要做什么。
答案 0 :(得分:1)
该功能无法返回:
function GetTMS(yearMetricCombo, modelData){
// NO RETURN STATEMENT HERE
modelData
.query("11111")
.where("fymcl").equals(yearMetricCombo) // tm|FY18|Promtions
.loadAll()
.exec((error, data) => {
console.log("error",error);
if (error) {
} else {
let tms = String(data.Items[0].attrs.uploadtms);
console.log("tms",tms);
// this returns from the *arrow function*, not
// the enclosing function declaration
return tms;
}
});
// NO RETURN STATEMENT HERE
}
请参阅与@ CRice评论相关的问题,了解如何从异步调用中返回