I want to implement an upload file function and thus need to do an Axios post request, which requires a token for the user, to the server and after each upload is completed the token for the user is changed. So when the same is implemented for uploading multiple files the token gets changed after the first file upload is complete and thus the request for the second file gives an error as it already sent a request with the previous token.
I need to implement Axios requests for the upload function one at a time and store the requests in a queue and send them to the server one by one. How to implement this?
Edit: I don't need the entire code. I will be able to write the code but I want to know how can we implement it. I searched on google and the closest I could find something was of concurrency in promises. And I tried to fetch in a for loop and the same error appeared because I need to wait for the first fetch statement to complete and then get the new token and then request using that token. If I use the synchronous method then the UI will be stuck causing a bad user experience
axios({
method: "post",
url: `url`,
headers: {
"Content-type": "multipart/form-data",
token: localStorage.getItem("token"),
},
data: formData,}).then(response=>{localStorage.setItem("token", response.data.newtoken);})
I need to send this new token with the file being uploaded next.