2 回答
TA贡献1827条经验 获得超8个赞
发生这种情况是因为你内心checkParentLoggerLevel没有等待axios承诺完成。你可以这样做:
async checkParentLoggerLevel() {
alert('inside checkParentLoggerLevel ');
return await axios
.get('url')
.then((res) => {
return 'hello';
});
}
此外,您需要在内部等待updateLevel:
async updateLevel() {
axios
.post(url)
.then(async (res) => {
var data = await this.checkParentLoggerLevel();
alert("This will be executed before the second methods returns HEllo");
alert(data);
});
}
TA贡献2019条经验 获得超9个赞
你应该链接承诺,所以:
updateLevel() {
axios
.post(url)
.then(res => {
return this.checkParentLoggerLevel.then(data => ([res, data]);
})
.then(([res, data]) => {
// here
});
}
或者简单地使用异步等待:
async updateLevel() {
const res = await axios.post(url);
const data = await this.checkParentLoggerLevel();
// Do whatever you want
}
添加回答
举报