-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtryCatch.js
More file actions
61 lines (56 loc) Β· 2.02 KB
/
tryCatch.js
File metadata and controls
61 lines (56 loc) Β· 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"use strict";
//implementing the try catch in async function along with the error handling functions
const countriesContainer = document.querySelector(".countries");
const renderError = function (msg) {
countriesContainer.insertAdjacentText("beforeend", msg);
};
const renderCountry = function (data, className = "") {
const html = `<article class="country ${className}">
<img class="country__img" src="${data.flags.png}" />
<div class="country__data">
<h3 class="country__name">${data.name.common}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>π«</span>${(
data.population / 1000000
).toFixed(1)}M people</p>
<p class="country__row"><span>π£οΈ</span>${Object.values(
data.languages
).join(", ")}</p>
<p class="country__row"><span>π°</span>${
Object.values(data.currencies)[0].name
}</p>
</div>
</article>`;
countriesContainer.insertAdjacentHTML("beforeend", html);
countriesContainer.style.opacity = 1;
};
const getPosition = function () {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
};
const whereAmI = async function () {
try {
const pos = await getPosition();
console.log(pos);
const { latitude: lat, longitude: lng } = pos.coords;
const resGeo = await fetch(
`https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=${lat}&longitude=${lng}`
);
console.log(resGeo);
if (!resGeo.ok) throw new Error("PROBLEM GETTING THE LCOATION DATA");
const dataGeo = await resGeo.json();
console.log(dataGeo);
const res = await fetch(
`https://restcountries.com/v3.1/name/${dataGeo.countryName}`
);
if (!res.ok) throw new Error("PROBLEM GETTING THE COUNTRY");
const data = await res.json();
console.log(data);
renderCountry(data[0]);
} catch (err) {
console.error("this is an error", err);
renderCountry(`SOMETHING WENT WRONG π© ${err.message}`);
}
};
whereAmI();