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
}
添加回答
舉報