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

311
Visualizações
async await inside script - API call with fetch doesn't return result

I am trying to retrieve weather data from an API by using a JavaScript script on the client side. This is how the code looks like:

  <script>
    async function fetchWeatherData(){
      var url = "http://www.7timer.info/bin/api.pl?lon=-3.57&lat=40.49&product=astro&output=json";
      var weather_data = await fetch(url);
      var weather_json = await weather_data.json();
      return weather_json.dataseries[0].temp2m;
    }
    document.write(await fetchWeatherData());
  </script>

Unfortunately this code doesn't work as I expected. A very similiar Node.JS/Express works great on the server side:

export async function getTemperature(iata){
    let latLong = latLongAirports.find(el => el.iata === iata);
    var url = "http://www.7timer.info/bin/api.pl?lon=" + latLong.long + "&lat=" + latLong.lat + "&product=astro&output=json"
    const response = await fetch(url);
    const jsonTemperature = await response.json();
    return jsonTemperature.dataseries[0].temp2m;
}

What am I missing or not understanding between the client/server side codes?

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

0

the problem ist that you can´t use "top level async" in normal script tags. you need to declare your script tag as type="module". Then it will work like you expect it.

      <script type="module">
        async function fetchWeatherData(){
          var url = "http://www.7timer.info/bin/api.pl?lon=-3.57&lat=40.49&product=astro&output=json";
          var weather_data = await fetch(url);
          var weather_json = await weather_data.json();
          return weather_json.dataseries[0].temp2m;
        }
        document.write(await fetchWeatherData());
      </script>

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