我有2个发布请求,如果第一个请求被触发,则第二个必须等待响应并执行发布请求。我读了一些有关等待和异步的信息,但是不好。
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(){
//
});
答案 0 :(得分:1)
您可以将第二个呼叫与第一个请求的.then链接
axios.post('/whatever').then((response) => {
return axios.post('/whatever2');
}).then((response) => {
console.log('Response', response);
});
答案 1 :(得分:1)
您可以像这样简单地使用async / await:
async myMethod() {
let response1 = await axios.post('/1', ...)
let response2 = await axios.post('/2', ...)
}