Sometimes, we want to return data from axios API with JavaScript.
In this article, we’ll look at how to return data from axios API with JavaScript.
How to return data from axios API with JavaScript?
To return data from axios API with JavaScript, we can use the return
statement.
For instance, we write
const axiosTest = async () => {
const response = await axios.get(url);
return response.data;
};
to define the axiosTest
function that returns the response data from the axios.get
method.
We call axios.get
with url
to return a promise with the response from making a get request to the url
.
Then we return response.data
to return the data.
Next, in an async function, we write
const data = await axiosTest();
to get the response data from axiosTest
.
Conclusion
To return data from axios API with JavaScript, we can use the return
statement.
One reply on “How to return data from axios API with JavaScript?”
Just saying;
const data = await axiosTest();
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules