Skip to content

Commit db0e391

Browse files
committed
final commit
1 parent ecd2acb commit db0e391

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

error-handling-01/script.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3634
const 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'));

remove-callbacks-es6/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const getCountryInfo = function (country) {
5656
);
5757
});
5858
};
59-
getCountryInfo('brazil');
59+
getCountryInfo('russia');
6060

6161
///////////////////////My Understanding of chaining and the challenge////
6262

0 commit comments

Comments
 (0)