Skip to content

Instantly share code, notes, and snippets.

@unes
Created August 28, 2023 09:54
Show Gist options
  • Save unes/3a0cc9b441f49d2275d750c2a682a350 to your computer and use it in GitHub Desktop.
Save unes/3a0cc9b441f49d2275d750c2a682a350 to your computer and use it in GitHub Desktop.
get weather info of a random city. using : async, await, fetch
const cities = [
{ name: 'New York', lat: 40.7128, lng: -74.0060 },
{ name: 'London', lat: 51.5074, lng: -0.1278 },
{ name: 'Paris', lat: 48.8566, lng: 2.3522 },
{ name: 'Tokyo', lat: 35.6895, lng: 139.6917 },
{ name: 'Sydney', lat: -33.8651, lng: 151.2099 },
{ name: 'Rome', lat: 41.9028, lng: 12.4964 },
{ name: 'Cairo', lat: 30.0444, lng: 31.2357 },
{ name: 'Rio de Janeiro', lat: -22.9068, lng: -43.1729 },
{ name: 'Dubai', lat: 25.2048, lng: 55.2708 },
{ name: 'Rabat', lat: 34.0209, lng: -6.8416 }
];
function selectRandomCity(cities) {
const randomIndex = Math.floor(Math.random() * cities.length);
return cities[randomIndex];
}
async function getWeather(city) {
const response = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${city.lat}&longitude=${city.lng}&current_weather=true`);
const data = await response.json();
console.log('Data:', data);
}
getWeather(selectRandomCity(cities));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment