我正在await
进行一些API回调,这些回调返回带有注销回调的401响应,并重定向到登录页面。但是,它会继续在注销回调上执行window.location.reload,因为在上一页中一次又一次地调用了API。我该怎么办才能解决这个问题?
try {
// Run all actions here that needed to be executed at first
await this.someMethods()
await this.someMethods()
await this.someMethods()
await this.someMethods()
await this.someMethods()
} catch (error) {
// Check for login session
const response = Object.assign({}, error)
if (response.response.status === 401 || response.response.status === 401) {
AuthService.logout() //LOGOUT CALLBACK
this.$toasted.show('Season Expired!. Please Re-Login', {
position: 'top-center',
duration: 2500,
type: 'error'
})
}
}
logout () {
window.localStorage.clear()
window.location.reload(true)
router.push('/login')
}
答案 0 :(得分:0)
可能您需要使用Promise.all
Promise.all([promise1, promise2, promise3]).then(function(values) {
console.log(values);
});
这是你的例子
try {
// Run all actions here that needed to be executed at first
await Promise.all([this.someMethods1() , this.someMethods2() ,this.someMethods3()]) ;
} catch (error) {
// Check for login session
const response = Object.assign({}, error)
if (response.response.status === 401 || response.response.status === 401) {
AuthService.logout() //LOGOUT CALLBACK
this.$toasted.show('Season Expired!. Please Re-Login', {
position: 'top-center',
duration: 2500,
type: 'error'
})
}
}
这样,错误块将仅输入一次