-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
308 lines (219 loc) · 10.3 KB
/
script.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict';
const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector('.countries');
/*
/////////////////////////////////////// Ajax call ///////////////////////////////////////
const getCountryData = function (country) {
const request = new XMLHttpRequest();
request.open('GET', `https://restcountries.com/v3.1/name/${country}`);
request.send();
request.addEventListener('load', function () {
const [data] = JSON.parse(this.responseText);
console.log(data);
// const lang = data.languages;
// const langspoken = Object.values(data.languages)[0]
// const currencies = Object.values(data.currencies)[0].name;
const html = `
<article class="country">
<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)} people </p>
<p class="country__row"><span>🗣️</span>${Object.values(data.languages)[0]}</p>
<p class="country__row"><span>💰</span>${Object.values(data.currencies)[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
})
}
getCountryData('portugal');
getCountryData('India')
getCountryData('usa')
*/
/////////////////////////////////////// Callback hell ///////////////////////////////////////
// multiple ajax call , let's create a sequence of calls , so the second will run after the first get complete
// I'm going to reder the neighboring country , besides the origianl country
const renderCountry = function (data) {
// const flagsImgage = Object.values(data.flags)[0].png;
const html = `
<article class="country">
<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)} people </p>
<p class="country__row"><span>🗣️</span>${Object.values(data.languages)[0]}</p>
<p class="country__row"><span>💰</span>${Object.values(data.currencies)[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
// console.log(data.flags.png);
}
// const getCountryAndNeighbor = function (country) {
// // AJAX CAll country 1
// const request = new XMLHttpRequest();
// request.open('GET', `https://restcountries.com/v3.1/name/${country}`); // first AJAX call
// request.send();
// request.addEventListener('load', function () {
// const [data] = JSON.parse(this.responseText);
// console.log(data);
// //render Country 1
// renderCountry(data);
// // Get neighbor Country 2
// const [neighbor] = data.borders;
// if (!neighbor) return;
// // AJAX CAll country 2
// const request2 = new XMLHttpRequest();
// request2.open('GET', `https://restcountries.com/v3.1/alpha/${neighbor}`); // second AJAX call
// request2.send();
// request2.addEventListener('load', function () {
// const data2 = JSON.parse(this.responseText);
// console.log(data2);
// console.log(data2);
// renderCountry(data2, 'neighbor');
// })
// });
// // console.log(neighbor);
// }
// getCountryAndNeighbor('bangladesh');
// const getCountryAndNeighbour = function (country) {
// // AJAX call country 1
// const request = new XMLHttpRequest();
// request.open('GET', `https://restcountries.com/v3.1/name/${country}`);
// request.send();
// request.addEventListener('load', function () {
// const [data] = JSON.parse(this.responseText);
// console.log(data);
// // Render country 1
// renderCountry(data);
// // Get neighbour country (2)
// const [neighbour] = data.borders;
// if (!neighbour) return;
// // AJAX call country 2
// const request2 = new XMLHttpRequest();
// request2.open('GET', `https://restcountries.com/v3.1/alpha/${neighbour}`);
// request2.send();
// request2.addEventListener('load', function () {
// const data2 = JSON.parse(this.responseText);
// console.log(data2);
// renderCountry(data2, 'neighbour');
// });
// });
// };
// // getCountryAndNeighbour('portugal');
// getCountryAndNeighbour('usa');
// const getCountryData = function (country) {
// const request = new XMLHttpRequest();
// request.open('GET', `https://restcountries.com/v3.1/name/${country}`);
// request.send();
// const request = fetch('https://restcountries.com/v3.1/name/india');
// console.log(request);
/////////////////////////////////////// Consuming Promises ///////////////////////////////////////
// const getCountryData = function (country) {
// fetch(`https://restcountries.com/v3.1/name/${country}`)
// .then(function (response) {
// console.log(response)
// return response.json();//with json method we can read the data body
// // response.json will be the new promise, so we need to handle that promise as well . so
// // the way we do that is to call another then method .then(function(data){console.log(data);})
// }).then(function (data) {
// console.log(data);
// renderCountry(data[0]);
// })
// // to handle the fulfilled state we use 'then' method that is available on all Promises
// // we need to pass a callback function that we want ot executed as soon as the promise is actually fulfilled
// };
// const getCountryData = function (country) {
// fetch(`https://restcountries.com/v3.1/name/${country}`)
// .then((response) => response.json()//with json method we can read the data body
// // response.json will be the new promise, so we need to handle that promise as well . so
// // the way we do that is to call another then method .then(function(data){console.log(data);})
// ).then(data => renderCountry(data[0]));
// }
// getCountryData('india')
/////////////////////////////////////// Chanining Promises ///////////////////////////////////////
// const getCountryData = function (country) {
// //country 1
// fetch(`https://restcountries.com/v3.1/name/${country}`)
// .then((response) => response.json() // then method always return a promise , no matter if we acturally return anything or not
// // but if we don return value , then that value will became the fulfillment value of the return promise
// ).then(data => {
// renderCountry(data[0]);
// const neighbor = data[0].borders[0]
// if (!neighbor) return;
// // country 2
// return fetch(`https://restcountries.com/v3.1/alpha/${neighbor}`)
// })
// .then(response => response.json())
// .then(data => renderCountry(data, 'neighbor'));
// };
// btn.addEventListener('click', function () {
// getCountryData('india');
// })
/////////////////////////////////////// Handling Rejected Promises ///////////////////////////////////////
// the only way in which the fetch promise rejects is when the user looses its internet connection
// const renderError = function (msg) {
// countriesContainer.insertAdjacentText("beforeend", msg);
// // countriesContainer.style.opacity = 1;
// }
// const getCountryData = function (country) {
// //country 1
// fetch(`https://restcountries.com/v3.1/name/${country}`)
// .then((response) => response.json() // then method always return a promise , no matter if we acturally return anything or not
// // but if we don return value , then that value will became the fulfillment value of the return promise
// ).then(data => {
// renderCountry(data[0]);
// const neighbor = data[0].borders[0]
// if (!neighbor) return;
// // country 2
// return fetch(`https://restcountries.com/v3.1/alpha/${neighbor}`)
// })
// .then(response => response.json())
// .then(data => renderCountry(data, 'neighbor'))
// .catch(err => {
// console.error(`${err} 🔥🔥`);
// renderError(`something went wrong 🔥🔥 ${err.message}. Try Again!`);
// })
// // we use this method for something that always needs to happen no matter the result of the promises
// .finally(() => {
// countriesContainer.style.opacity = 1;
// })
// };
// btn.addEventListener('click', function () {
// getCountryData('india');
// })
//since ES2017 there is now an even better and easier way to consume promises which is called async and await
/////////////////////////////////////// Consuming Promises with Async / Await ///////////////////////////////////////
// so now this function is asynchronous function
// // will basically keep running in the background
// const wehereAmI = async function (country) {
// }
const getPosition = function () {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
})
}
const wehereAmI = async function (country) {
// we use the await keyword to basically await for the result of this promises
// Isn't stopping the Code Blocking the Execution ?
// ans no bua , stopping execution in an async function , the asunchronous function Run Ascynchrouslly in background
// not blocking the main threat of exection , not blocking the call stack
const pos = await getPosition();
const { latitude: lat, longitude: lng } = pos.coords;
// Reverse geocoding
const resGeo = await fetch(`https://geocode.xyz/${lat},${lng}?geoit / =json`);
const dataGeo = await resGeo.json();
console.log(dataGeo);
// const { } = await getPosition();
const res = await fetch(`https://restcountries.com/v3.1/name/${country}`)
const data = await res.json();
console.log(data);
renderCountry(data[0]);
}
wehereAmI('india');
console.log('first');