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

326
Vistas
How to get all the data with Promise.all in the useEffect hook?

I have this useEffect hook, that is trying to fetch all the data in one time

  useEffect(() => {
    let promises = [];
    data.forEach(item => {
      promises.push(fetch(`https://app.subsocial.network/subid/api/v1/check/${(item.name).toLowerCase()}`))
    });

    Promise.all(promises)
      .then(response => response.map(item => item.json()))
      .then(data => console.log(data))
      .catch(error => console.log(error))
  }, [data]);

But, instead the data I have this in console:

enter image description here

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

The json() method returns a promise.

This means that you've used Promise.all to wait for all the response promises to resolve, but then you just generate a new bunch of JSON parsing promises instead.

Generate the JSON parsing promises before you use Promise.all.

i.e.

promises.push(
    fetch(`https://app.subsocial.network/subid/api/v1/check/${(item.name).toLowerCase()}`))
    .then(response => response.json());
about 4 years ago · Santiago Gelvez Denunciar

0

You are almost there, you just need to analyze every promise that you get the first Promise.all

useEffect(() => {
  let promises = [];
  data.forEach((item) => {
    promises.push(
      fetch(
        `https://app.subsocial.network/subid/api/v1/check/${item.name.toLowerCase()}`
      )
    );
  });

  Promise.all(promises)
    .then((responses) => {
      // Take all responses and analyze them with another Promise.all
      return Promise.all(
        responses.map((response) => {
          return response.json();
        })
      );
    })
    .then((data) => {
      // Extracted data
      console.log(data);
    })
    .catch((error) => {
      // Catched errors
      console.log(error);
    });
}, [data]);

In then method you are getting all responses, and wrapping them with yet another Promise.all so that you can extract from them what they receive.

about 4 years ago · Santiago Gelvez 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