@@ -25,12 +25,10 @@ const displayCountries = function (data, className = '') {
2525 countriesContainer . insertAdjacentHTML ( 'beforeend' , html ) ;
2626} ;
2727
28+ //Step 1
2829//////////////////////Shorter ES6 Syntax /////////////////////////////
29- const handlErr = function ( err ) {
30- countriesContainer . insertAdjacentText (
31- 'beforeend' ,
32- `Something went wrong. ${ err . message } Shitt Mannn 😲😲😲`
33- ) ;
30+ /* const handlErr = function (err) {
31+ return `Something went wrong. ${err.message} Shitt Mannn 😲😲😲`
3432};
3533
3634const getCountryInfo = function (country) {
@@ -46,10 +44,52 @@ const getCountryInfo = function (country) {
4644 .then(data => {
4745 displayCountries(data, 'neighbour');
4846 })
49- . catch ( err => handlErr ( err ) )
47+ .catch(err => countriesContainer.insertAdjacentText('afterbegin', handlErr(err) ))
5048 .finally(() => {
5149 countriesContainer.style.opacity = 1;
5250 });
5351};
5452
55- btn . addEventListener ( 'click' , e => getCountryInfo ( 'paktan' ) ) ;
53+ btn.addEventListener('click', e => getCountryInfo('pakistan')); */
54+
55+ //////////////////////////////////////////////////////////////////////////
56+ /////////////////////////////////////////////////////////////////////////
57+ //Step 2
58+ const handlErr = function ( err ) {
59+ console . log ( err ) ;
60+ return `Something went wrong. ${ err . message } Shitt Mannn 😲😲😲` ;
61+ } ;
62+
63+ const getCountryInfo = function ( country ) {
64+ fetch ( `https://restcountries.eu/rest/v2/name/${ country } ` )
65+ . then ( response => {
66+ if ( ! response . ok )
67+ throw new Error (
68+ `|${ country } | NOT FOUND ${ response . status } `
69+ ) ;
70+ return response . json ( ) ;
71+ } )
72+ . then ( ( [ data ] ) => {
73+ displayCountries ( data ) ;
74+ const neighbour = data . borders [ 0 ] ;
75+ //then method always return the promises so always return from last then()
76+ return fetch ( `https://restcountries.eu/rest/v2/alpha/${ neighbour } ` ) ;
77+ } )
78+ . then ( response => {
79+ if ( ! response . ok )
80+ throw new Error (
81+ `|${ country } | NOT FOUND ${ response . status } `
82+ ) ;
83+ return response . json ( )
84+ } )
85+ . then ( data => {
86+ displayCountries ( data , 'neighbour' ) ;
87+ } )
88+ . catch ( err =>
89+ countriesContainer . insertAdjacentText ( 'beforeend' , handlErr ( err ) )
90+ )
91+ . finally ( ( ) => {
92+ countriesContainer . style . opacity = 1 ;
93+ } ) ;
94+ } ;
95+ btn . addEventListener ( 'click' , e => getCountryInfo ( 'pakistanlm' ) ) ;
0 commit comments