I have create a function that uploads a image wist axios allong with text to my API
everything works fine. but after i have uploaded it i direclty I want to call the “getData()” function to direclty show the new created data in a table. but its not working direclty after
when i refresh the page it works fine and i see the new data because i use “componentDidMount()” with the getData function:
componentDidMount() {
this.getData();
}
why does it not work on the "onFileUpload " as well
is it because the data has not been uploaded yet when i call the “getData”
function. How do i solve this?
Right, so the POST call is asynchronous. In other words, there is no guarantee when it will be done. It will happen “later”. Because of that, JS will just move onto the next instruction, it won’t wait.
If you need that data, you need to handle it after you know that the POST is returned. If I remember correctly, that function returns a Promise, meaning you can either handle it with a Promise or with async/await.
Here are some examples on how to handle an async call. The only difference is that you need to chain them, but that should be easy to find with a google search.