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

109
Visualizações
Getting an array of Promises after async await

i'm trying to create an array of the daily forecast mapping over an array with cities. I'm trying to map over the array of the cities making an api call for each one of them once the page loads. I keep on getting an array of Promises such as this : enter image description here

Favorite Page component:

  const [dailyForeCast, setDailyForeCast] = useState([]);

  const favorites = [
    {
      Key: '213181',
      type: 'City',
    },
    {
      Key: '213121',
      type: 'City',
    },
  ];


  useEffect(() => {
    const fetchData = async () => {
      const results = await favorites.map((city) => {
        return weatherService.getSingleForeCast(city.Key);
      });
      setDailyForeCast(results);
    };

    fetchData();
  }, []);
  console.log('dailyForest:', dailyForeCast);

Service with the api call :

async function getSingleForeCast(value) {
  try {
    const res = await axios.get(`http://dataservice.accuweather.com/forecasts/v1/daily/1day/${value}`, {
      params: {
        apikey: API_KEY,
        details: true,
        metric: true,
      },
    });

    const result = res.data.DailyForecasts;

    return result;
  } catch {
    console.log('cant get single forecast');
  }
}

Would appreciate any help ,thanks in advance :)

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

0

As Brian has said you're not actually awaiting the promises, but the map function.

Try something like this (untested):

    const fetchData = async () => {
      const promises = favorites.map((city) => {
        return weatherService.getSingleForeCast(city.Key);
      });
      const results = await Promise.all(promises)
      setDailyForeCast(results);
    };
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