Skip to content

Commit 88c6585

Browse files
committed
even better js example
1 parent 4a94242 commit 88c6585

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

doc/assets/getForecast.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
function getForecast() {
22
let lat = document.coords.lat.value;
33
let lon = document.coords.lon.value;
4-
fetch_json('https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=' + lat + '&lon=' + lon).then(
5-
function(value) {
6-
if (value != null) {
7-
document.getElementById('output').value = JSON.stringify(value,null,2);
8-
} else {
9-
document.getElementById('output').value = 'Failure';
10-
}
4+
let url = 'https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=' + lat + '&lon=' + lon;
5+
fetch(url).then( function(res) {
6+
if (res.ok) {
7+
res.json().then( function(data) {
8+
document.getElementById('output').value = JSON.stringify(data,null,2);
9+
});
10+
} else {
11+
document.getElementById('output').value = res.status;
1112
}
12-
);
13-
}
14-
15-
async function fetch_json(url) {
16-
let obj = null;
17-
try {
18-
obj = await (await fetch(url)).json();
19-
} catch(e) {
20-
console.log(e, url);
21-
}
22-
return(obj);
13+
}, function(e) {
14+
console.log("Fetch failed!", e);
15+
document.getElementById('output').value = e;
16+
});
2317
}
2418

2519
// Usage in HTML

0 commit comments

Comments
 (0)