Beusebiu
Beusebiu

Reputation: 1513

Post request with axios one after another Vuejs

I have 2 post request, if the first one is trigger the second one must await for the response and the do a post request. I read something about await and async, but no good.

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(){
    //
});

Upvotes: 1

Views: 1893

Answers (2)

TommyF
TommyF

Reputation: 7150

You can simply use async/await like this:

async myMethod() {
  let response1 = await axios.post('/1', ...)
  let response2 = await axios.post('/2', ...)
}

Upvotes: 1

cptnk
cptnk

Reputation: 2420

you can just chain the second call with .then of the first request

axios.post('/whatever').then((response) => {
    return axios.post('/whatever2'); 
}).then((response) => {
    console.log('Response', response);
});

Upvotes: 1

Related Questions