JavaScript Tutorial

JavaScript Fetch API

Source: Programming with Mosh

HTTP Requests — OpenWeatherMap

fetch() is how browsers call REST APIs. Check res.ok, parse JSON, and handle HTTP error codes — the same flow used in production dashboards.

JAVASCRIPT
async function getWeather(city) {
  const res = await fetch(
    `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=demo`
  );
  const data = await res.json();
  if (!res.ok) throw new Error(data.message);
  return data;
}
GET current weather

Console

// Click Run — watch API traces and console output