Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda