我有两个帖子请求,如果第一个是触发,第二个必须等待响应和做一个帖子请求。我读到了一些关于等待和异步的东西,但是没有什么好的。
let changed =false;
if(//something then){
changed = true;
axios.post('/uploadfile/' + id, // This must be first
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
}
})
.then(function(response){
// get response and do something
})
.catch(function(){
//
});
}
// if changed = true, then await for response from first post
axios.post('/uploadfile' + this.id_contract, // If
data,
{
headers: {
Authorization: getToken()
}
})
.then(function(){
// do something else
})
.catch(function(){
//
});
发布于 2020-02-16 13:52:16
您可以像这样简单地使用异步/等待:
async myMethod() {
let response1 = await axios.post('/1', ...)
let response2 = await axios.post('/2', ...)
}
发布于 2020-02-14 10:01:17
您可以使用第一个请求的.then链接第二个调用
axios.post('/whatever').then((response) => {
return axios.post('/whatever2');
}).then((response) => {
console.log('Response', response);
});
https://stackoverflow.com/questions/60223043
复制相似问题