Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

329
Vistas
Is there a way to make an api call within a map of another api call?

I know the title is quite confusing, I wasn't sure how to word it better. What I am trying to do is to fetch some items, map through those items to display them, but the problem is that one of those items has a value of what needs to be another api call to access it.

This is what I'm trying to do:

First of all I am storing an empty state, which later on becomes the data of the fetched items:

const [data, setData] = useState([]);

I'm using axios to fetch and store the data:

const fetchItems = () => {
  axios("https://swapi.dev/api/people")
    .then((response) => {
      console.log(response.data.results);

      const newData = response.data.results.map((item) => ({
        name: item.name,

        homeworld: () => {
          axios.get(item.homeworld).then((response) => {
            response.data.results;
          });
        },
      }));
      setData(newData);
    })

    .catch((error) => {
      console.log("error", error);
    });
};

It works with the name because it's a simple value. However, the homeworld includes a link that needs to be called once again in order to access it, instead of being a simple value like the name in this case. How can I call it and access what values are held within that link, and display them instead of just displaying the url?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I hope this can help you:

const [data,setData] = useState([])
const fetchItems = () => {
  axios("https://swapi.dev/api/people")
    .then(response => {
      console.log(response.data.results);

      const { results } = response.data;

      for (const item of results) {
         axios.get(item.homeworld).then(({data}) => {
          setData([...data,{ name: item.name, homeworld: data.results }]);
        });
      }
    })
    .catch(error => {
      console.log("error", error);
    });
};

or with fetch:

const [data,setData] = useState([])
fetch("https://swapi.dev/api/people").then(re=>re.json())
    .then(response => {
      const newData = []
      const { results } = response;
      const newData = [];
      for (const item of results) {
         fetch(item.homeworld).then(re => re.json()).then((data) => {
             newData.push({ name: item.name, homeworld: data });
         });
      }
      console.log(newData)
      setData(newData)
    })
    .catch(error => {
      console.log("error", error);
    });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Promise.all()

You can use Promise.all() method to get all the information you need by creating an array of promises by mapping the response.results array with an async function.

This is the code example

const fetchItems = async () => {
  const req = await axios.get("https://swapi.dev/api/people");
  const response = await req.data;

  const allDataPromises = response.results.map(async (item) => {
    const itemReq = await axios.get(item.homeworld);
    const itemResponse = await itemReq.data;
    return {
      name: item.name,
      homeworld: itemResponse,
    };
  });

  const allData = await Promise.all(allDataPromises);
};

For further information about Promise.all()

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda