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.84k stars 16.88k forks source link

Chaining promise issue in Country API (Solution) #437

Open saroshkhann opened 2 months ago

saroshkhann commented 2 months 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 1 month ago

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