search

Home  >  Q&A  >  body text

I want to make my async calls more readable

I currently handle my promises by calling an async function and chaining it with .then(). But I wish there was a more readable way.

My current feasible method is:

const apiCall = async() => {
  const response = await axios.get("URL");
  return response;
}

apiCall().then(res => {
  console.log(res.data);
});

I want my code to look like:

const apiCall = () => {
  const response = axios.get("URL);
  return response;
}

const fetchData = async() => {
  const response = await apiCall();
  return response.data;
}

console.log(fetchData());
P粉573943755P粉573943755233 days ago3807

reply all(1)I'll reply

  • P粉726234648

    P粉7262346482024-04-06 09:27:18

    how

    const apiCall = async() => {
      const { data } = await axios.get("URL");
      return data;
    }

    reply
    0
  • Cancelreply