forked from HackYourFuture/JavaScript3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (34 loc) · 1.02 KB
/
app.js
File metadata and controls
40 lines (34 loc) · 1.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
/*
Replace then/catch method chain with async/await and try/catch
*/
'use strict';
{
async function fetchJSON(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Network error: ${response.status} - ${response.statusText}`);
}
return response.json();
}
async function fetchAndRender(url) {
const pre = document.getElementById('response');
try {
const data = await fetchJSON(url);
pre.textContent = JSON.stringify(data, null, 2);
} catch (err) {
pre.textContent = err.message;
}
}
function main(url) {
const button = document.getElementById('btn-go');
button.addEventListener('click', () => fetchAndRender(url));
const span = document.getElementById('counter');
let counter = 0;
setInterval(() => {
counter += 1;
span.textContent = counter;
}, 200);
}
const NOBEL_PRIZE_API_END_POINT = 'http://api.nobelprize.org/v1/laureate.json?gender=female';
window.onload = () => main(NOBEL_PRIZE_API_END_POINT);
}