1 回答

TA贡献1825条经验 获得超4个赞
在我看来,refreshToken 函数与您的 login 函数并行运行,因为您不必等到 refreshToken 函数完成。您可以尝试将第一个更改为异步函数并等待结果。
export const add_account = async account=>{
var token=localStorage.getItem("access_token")
var decoded=jwt_decode(token);
var time_exp=decoded.exp;
if(time_exp<new Date().getTime()/1000) {
await refreshToken();
}
return axios.post("http://localhost:5000/accounts",{
acc_name:account.name,
acc_pass:account.pass,
acc_host:account.host,
acc_description:account.description
}, {headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token')}`
}}).then(res=>{
return res.data
})
}
也许在风格上也使用 then 语法而不是 async await 会更有意义,但是您必须指定两条执行路径(一条带有 refreshToken,一条不带),这看起来很复杂。
添加回答
举报