jonasschmedtmann / complete-javascript-course

Starter files, final projects, and FAQ for my Complete JavaScript course
https://www.udemy.com/the-complete-javascript-course/?couponCode=C3GITHUB10
15.69k stars 16.81k forks source link

Chaining promise issue in Country API (Solution) #437

Open saroshkhann opened 1 month ago

saroshkhann commented 1 month ago

/////////// Instead of this code

const getCountryData = function (country) {
  // Country 1
  fetch(`https://restcountries.com/v3.1/name/${country}`)
    .then(response => response.json())
    .then(data => {
      renderCountry(data[0]);
      const neighbour = data[0].borders[0];
      if (!neighbour) return;

      return fetch(`https://restcountries.com/v3.1/alpha/${neighbour}`);
    })
    .then(response => response.json())
    .then(data => renderCountry(data, 'neighbour'));
};

////////////// Write this code


const getCountryData = function (country) {
  // Country 1
  fetch(`https://restcountries.com/v3.1/name/${country}`)
    .then(response => response.json())
    .then(data => {
      renderCountry(data[0]);
      const neighbour = data[0].borders[0];
      if (!neighbour) return;

      return fetch(`https://restcountries.com/v3.1/alpha/${neighbour}`);
    })
    .then(response => response.json())
    .then(data => renderCountry(data[0], 'neighbour'));
};
Hamza-jamshaid commented 6 days ago

describe it clearly. file name and What is the issue that it had ?