Categories
JavaScript Answers

How to return data from axios API with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.