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

106
Visualizações
return undefined on fetch request inside map react

I tried to get a map array from fetch resolves so on every element inside favoriteCards it returns a value and assigns it in test

useEffect(() => {
  const test = favoriteCards.map((card) => {
    accuWeatherApi.getCurrentWeather(card.key).then((res) => {
      return res;
    });
  });
  setRenderFavorites(test);
}, [favoriteCards]);

console.log(renderFavorites)

I always get undefined no matter what I do

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The getCurrentWeather operation is asynchronous. So in this case you need to await a series of operations to get their results before you can set those results to a state value.

In a useEffect that might look something like this:

useEffect(() => {
  const fetchWeather = async () => {
    // begin the asynchronous operations
    const promises = favoriteCards.map(card => accuWeatherApi.getCurrentWeather(card.key));

    // wait for all of them to complete
    const results = await Promise.all(promises);

    // update state with the results
    setRenderFavorites(results);
  }

  fetchWeather();
}, [favoriteCards]);

Though as you might imagine, there are a variety of ways to do this. For example, the above code performs all of the asynchronous operations right away and the results will be in whatever order they finish. If you need them to be in the same order, you might await each operation one at a time. For example:

useEffect(() => {
  const fetchWeather = async () => {
    const results = [];

    // await each operations in order
    for (const card of favoriteCards) {
      results.push(await accuWeatherApi.getCurrentWeather(card.key));
    }

    // update state with the results
    setRenderFavorites(results);
  }

  fetchWeather();
}, [favoriteCards]);

Note that in each case the function passed to useEffect itself is not async. But since asynchronous operations need to be awaited, internally that function defines a function which is async and then invokes that function. Internal to that function is where you'd await the operations.


As an aside, it's also worth pointing out that there was a second issue in your code... The syntax of your use of .map() explicitly doesn't return anything. When curly braces are used in the function passed to .map() then there is no default return and you need to explicitly return a value. So even if you were awaiting the operations or if they weren't asynchronous in the first place, it would still be undefined because there's no return value.

about 4 years ago · Juan Pablo Isaza 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