Javascript 如何在VueJS方法中继续之前等待Axios请求?
我有以下函数,当用户单击HTML元素时触发Javascript 如何在VueJS方法中继续之前等待Axios请求?,javascript,vue.js,async-await,axios,Javascript,Vue.js,Async Await,Axios,我有以下函数,当用户单击HTML元素时触发 async upvote(news) { await this.isAuthors(news) if (this.user != null) { axios.post(serverSide.findUserByID, {userID: this.user._id}) .then((res) => { this.user.rep = res.data.user.rep if (this.votingL
async upvote(news) {
await this.isAuthors(news)
if (this.user != null) {
axios.post(serverSide.findUserByID, {userID: this.user._id})
.then((res) => {
this.user.rep = res.data.user.rep
if (this.votingList[news._id].isAuthors == true) {
alert('Authors of the same publisher is not allowed to vote this news!')
return
}
else () {
// some other Axios request here
}
})
}
else {
this.$router.push({name: 'login'})
}
}
但是,整个逻辑取决于在发出另一个Axios请求之前正在处理的值this.isAuthors(news)
,因此可以将this.votingList[news.\u id].isAuthors
设置为其默认值。下面是获取所述值的函数
async isAuthors(news) {
if (this.user.hasPublisher == true) {
axios.post(serverSide.getPublisher, {
userID: this.user._id, userRole: this.user.role
})
.then((res) => {
if (news.publisher == res.data.publisher._id) {
console.log("Same publishers")
this.votingList[news._id].isAuthors = true
}
else {
this.votingList[news._id].isAuthors = false
}
})
}
else {
this.votingList[news._id].isAuthors = false
}
}
如何使
upvote(news)
在继续函数之前等待isAuthors(news)
获取的值?因为到目前为止,this.votingList[news.\u id].isAuthors
仍将保持其默认值您必须在isAuthors
函数中等待axios.post。