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

124
Views
Promesas con errores de búsqueda y lanzamiento

Tengo un problema cuando arrojo un error en el primer bloque .then, el bloque .catch no detecta el error que se arrojó. Simplemente muestra el error en el bloque .catch.

Aquí hay un enlace al código: Code Pen Example

 const getCountryData = country => { // Country 1 fetch(`https://restcountries.com/v2/name/${country}`) .then(response => { if (!response.ok) throw new Error(`Country not found (${response.status})`); return response.json(); }) .then(data => { renderCountry(data[0]); const neighbor = data[0].borders[0]; if (!neighbor) return; // Country 2 return fetch(`https://restcountries.com/v2/alpha/${neighbor}`); }) .then(response => { if (!response.ok) throw new Error(`Country not found (${response.status})`); return response.json(); }) .then(data => { renderCountry(data, 'neighbour'); }) .catch(err => { console.error(`${err} 🔥 `), renderError(`Something went wrong ${err.message}. Try again!`); }) .finally(fade => { countriesContainer.style.opacity = 1; }); }; btn.addEventListener('click', function () { getCountryData('portu=gal'); });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Cuando no se encuentra el país, response.ok es verdadero pero el dato[0] no está definido. Eso es porque response.json da {status:404,message:”not found”} . Por lo tanto, la verificación de si se encuentra el país debe cambiar a

 If (data.status === 404) throw new Error(“not found”)
about 4 years ago · Juan Pablo Isaza 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!