Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Manejar el error cuando la promesa no se cumple

Estoy tratando de extraer datos del caché cuando la aplicación está fuera de línea e imprimir "Sin conexión a Internet", pero sigo sin detectar un error cuando la Promesa no se cumple, es decir, cuando la aplicación está fuera de línea. Probé try/catch, response.catch y otras lógicas, pero ninguna funcionó. ¿Podría alguien señalarme la dirección correcta? ¿Qué está mal con mi código?

 grad = input.value ? input.value : "zagreb"; try{ let response = await fetch( `https://api.weatherapi.com/v1/forecast.json?key=bdbf7197ebc24abbb15104932220407&q=${grad}&aqi=yes`) //console.log("API-response: ", response) let data = await response.json(); if (data.error) { document.getElementById("content").innerHTML = `<p id="nodata">No data for the requested city</p>`; } else { dayTime(data); let rezultat = `<p id="location">${data.location.name}, ${data.location.country}, ${data.location.localtime}</p><ul id="lista">`; rezultat += weatherConditions(data); rezultat += temperatura(data); rezultat += cloudCover(data); rezultat += uvZracenje(data); rezultat += "</ul>"; document.getElementById("content").innerHTML = rezultat; } } catch{ let cashed = ``; data.cache = response.headers.has("cache"); console.log("kesiran data: ", data.cache) if(data.cache){ cashed = document.getElementById("content").innerHTML = `<p id="nointernet">No internet connection</p>`; } }
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Adjunte catch a fetch() en sí mismo, desde allí puede establecer un indicador o lo que sea que haya un error, la response no estará undefined para que también pueda verificar eso.

 async function myFunc() { console.log("fetching"); let response = await fetch(`https://whatever.com`) .catch(er => console.error("error", er)); console.log("API-response: ", response) } myFunc();
 ​.as-console-wrapper{top:0;max-height:unset!important;overflow:auto!important}

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!