如果上一次调用在重试下一次调用之前返回了特定的返回码,我想进行一次axios调用。像下面的片段。我尝试过axios-retry但是我只是设法重试请求,如果它失败了,在重试请求之前没有打电话。谢谢你的帮助
axios
.get('http://myapilink.com/users', {
headers: {
'access_token': access_token,
'Content-Type': 'application/json'
}
})
.then(function (response) {
// successfull call
})
.err(function (error) {
// fire an another axios call before retrying the GET request above
axios
.post('http://myapilink.com/refresh_token', {
headers: {
'refresh_token': refresh_token,
'Content-Type': 'application/json'
}
})
.then(function (response) {
// successfull call - retry the GET request above
})
.err(function (error) {
// handle manual user login because of expired refresh_token
})
})

答案 0 :(得分:0)
将代码包装在函数中:
const fn = () => axios
.get('http://myapilink.com/users', {
headers: {
'access_token': access_token,
'Content-Type': 'application/json'
}
})
.then(function (response) {
// successfull call
})
.err(function (error) {
// fire an another axios call before retrying the GET request above
axios
.post('http://myapilink.com/refresh_token', {
headers: {
'refresh_token': refresh_token,
'Content-Type': 'application/json'
}
})
.then(function (response) {
// successfull call - retry the GET request above
fn()
})
.err(function (error) {
// handle manual user login because of expired refresh_token
})
})
// execute it to start fetching
fn()
答案 1 :(得分:0)
基本上,如果您想要链接异步HTTP请求,那么最好处理每个步骤(假设您没有50个步骤)。对于反应,在伪代码中它就像:
Set state (loading first option -> true)
Axios -> send off first request
If first request in successful, set state (request success -> true)
else set state (loading second option -> true )
if second request is successful, set state (request success -> true)
else set state (http request overall -> failed)
然后在渲染功能中根据状态显示相应的消息(即,如果它尝试第一个说&#34;加载&#34;,如果它正在加载第二个备份一个说&#34;加载辅助资源&#34;,如果BOTH请求失败说&#34;请求失败&#34;并做任何你想要的错误处理,如果其中一个成功,那么做你需要的结果)< / p>