Show me: fetch – ordering with code
In the last lesson, you opened an API in your browser by hand. Now your JavaScript will do the same thing – with the fetch command:
const adresa = 'https://api.open-meteo.com/v1/forecast?latitude=49.19&longitude=16.61¤t_weather=true';
const odpoved = await fetch(adresa);
const data = await odpoved.json();
console.log(data.current_weather.temperature);
What’s happening here, line by line:
fetch(adresa)– send the request (the waiter heads to the kitchen).await– wait for it to come back. The internet takes a moment!odpoved.json()– convert the response into a JavaScript object.- Then you work with the data like any regular object: dot, dot, done.
await only works inside a function marked async, or in a <script type="module">. If your browser shows the error “await is only valid in async functions”, wrap your code like this:
async function nacti() {
// fetch and await go here
}
nacti();
Print the data on the page
The console is for programmers – a visitor wants to see the data on the page:
<p id="teplota">Loading…</p>
<script type="module">
const odpoved = await fetch('https://api.open-meteo.com/v1/forecast?latitude=49.19&longitude=16.61¤t_weather=true');
const data = await odpoved.json();
document.querySelector('#teplota').textContent =
`Right now it's ${data.current_weather.temperature} °C`;
</script>
Familiar stuff from Magician (querySelector, textContent) plus the new fetch. Nothing more.
Your turn 💪
- Add a paragraph with
id="teplota"and the script above to your website. - Change the coordinates to your own city.
- Add the wind speed to the display too (
data.current_weather.windspeed). - Bonus: when the temperature is below 0, add ”🥶 Bundle up!” to the text.
Check: The real current temperature appears on your page after it loads. If you turn off Wi-Fi and refresh the page, it stays on “Loading…” – proof that the data really comes from the internet.
What to take away from this lesson
fetch(adresa)sends the request,awaitwaits for the response..json()converts the response into an object you already know how to work with.- Data from the whole internet is now just one function away.
© 2026 Ing. Martin Polak / AlgoRhino · Content usage terms